Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用exec函数的php后台进程_Php_Exec_Background Process - Fatal编程技术网

使用exec函数的php后台进程

使用exec函数的php后台进程,php,exec,background-process,Php,Exec,Background Process,为了找到确切的答案,我搜索了很多,但没有找到任何答案 许多人提到,我们应该在命令结束时和不要等待响应。 例如,要在后台运行bg.php,建议: exec("/usr/bin/php bg.php &"); 但这对我不起作用。主脚本等待bg.php的完整执行 我还读了一些东西,在日志文件中写入bg.php输出,但我的背景脚本没有生成任何输出。它执行一些处理,然后在数据库中写入一些内容 我只想让我的脚本运行bg.php,不要等到它结束。 请帮助我包括正确的代码。您也必须将程序输出重新路

为了找到确切的答案,我搜索了很多,但没有找到任何答案

许多人提到,我们应该在命令结束时不要等待响应。
例如,要在后台运行bg.php,建议:

exec("/usr/bin/php bg.php &");  
但这对我不起作用。主脚本等待bg.php的完整执行

我还读了一些东西,在日志文件中写入bg.php输出,但我的背景脚本没有生成任何输出。它执行一些处理,然后在数据库中写入一些内容

我只想让我的脚本运行bg.php,不要等到它结束。


请帮助我包括正确的代码。

您也必须将程序输出重新路由到某个地方,通常是/dev/null

exec($cmd . " > /dev/null &");

你考虑过使用屏幕吗?您可以启动在分离进程中运行的屏幕会话

screen -d -m -S my_bg_session /usr/bin/php bg.php
然后,如果它仍在运行,您可以稍后连接到它:

screen -r my_bg_session

很好!但是什么是/dev/null目录?此命令是否将输出写入文件?/dev/null仅丢弃发送给它的数据,如果需要程序输出,也可以将其路由到某个文件