php background process using exec function

人盡茶涼 提交于 2019-12-03 07:58:20

问题


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.php in background , this was recommended:

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.

I also read somewhere to write bg.php output in a logfile but my background script doesn't produce any output. It does some process and then write something in database.

I just want my script to run bg.php and don't wait for it to end.

please help me including correct code.


回答1:


You have to reroute programs output somewhere too, usually /dev/null

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



回答2:


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


来源:https://stackoverflow.com/questions/12842767/php-background-process-using-exec-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!