php background process using exec function

Aliweb picture Aliweb · Oct 11, 2012 · Viewed 20.7k times · Source

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.

Answer

ninaj picture ninaj · Oct 11, 2012

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

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