php php后台进程使用exec函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12842767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php background process using exec function
提问by Aliweb
I have searched a lot to find the exact answer but didn't find any.
我已经搜索了很多以找到确切的答案,但没有找到任何答案。
many people mentioned that we should &at end of command to don't wait for response.
for example to run bg.phpin background , this was recommended:
很多人提到,我们应该&AT命令的结束并不等待回应。
例如在后台运行bg.php,建议这样做:
exec("/usr/bin/php bg.php &");
but it doesn't work for me. and the main script waits for complete execution of the bg.php.
但这对我不起作用。并且主脚本等待bg.php 的完整执行。
I also read somewhere to write bg.phpoutput in a logfile but my background script doesn't produce any output. It does some process and then write something in database.
我还在某处阅读了将bg.php输出写入日志文件的地方,但我的后台脚本没有产生任何输出。它会做一些处理,然后在数据库中写入一些东西。
I just want my script to run bg.php and don't wait for it to end.
我只是想让我的脚本运行 bg.php 而不要等待它结束。
please help me including correct code.
请帮助我包括正确的代码。
回答by ninaj
You have to reroute programs output somewhere too, usually /dev/null
您也必须在某处重新路由程序输出,通常是 /dev/null
exec($cmd . " > /dev/null &");
回答by moodboom
Have you considered using screen? You can start up a screen session that runs in a detached process.
您是否考虑过使用屏幕?您可以启动在分离进程中运行的屏幕会话。
screen -d -m -S my_bg_session /usr/bin/php bg.php
Then you can connect to it later if it is still running:
如果它仍在运行,您可以稍后连接到它:
screen -r my_bg_session

