php exec命令(或类似命令)不等待结果 [英] php exec command (or similar) to not wait for result

查看:416
本文介绍了php exec命令(或类似命令)不等待结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要运行的命令,但我不想让PHP坐下来等待结果.

I have a command I want to run, but I do not want PHP to sit and wait for the result.

<?php
echo "Starting Script";
exec('run_baby_run');
echo "Thanks, Script is running in background";
?>

是否可以让PHP不等待结果.即,只需将其启动并移至下一个命令即可.

Is it possible to have PHP not wait for the result.. i.e. just kick it off and move along to the next command.

我找不到任何东西,甚至不确定它是否可能.我所能找到的最好的方法是,有人可以在一分钟内完成CRON工作.

I cant find anything, and not sure its even possible. The best I could find was someone making a CRON job to start in a minute.

推荐答案

来自文档:

为了执行命令并使它在执行时不挂起您的PHP脚本
它运行时,您运行的程序不得输出回PHP.为此,
将stdout和stderr都重定向到/dev/null,然后对其进行后台处理.

In order to execute a command and have it not hang your PHP script while
it runs, the program you run must not output back to PHP. To do this,
redirect both stdout and stderr to /dev/null, then background it.

> /dev/null 2>&1 &

为了执行命令并具有
它作为另一个过程而产生
不依赖于Apache线程
保持运行(如果
有人取消页面)运行此命令:

In order to execute a command and have
it spawned off as another process that
is not dependent on the Apache thread
to keep running (will not die if
somebody cancels the page) run this:

exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');

这篇关于php exec命令(或类似命令)不等待结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆