php exec command (or similar) to not wait for result

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

It's always good to check the documentation:
You can run the command in the background by adding a & at the end of it as:
exec('run_baby_run &');
But doing this alone will hang your script because:
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
So you can redirect the stdout of the command to a file, if you want to see it later or to /dev/nullif you want to discard it as:
exec('run_baby_run > /dev/null &');

"exec nohup setsid your_command"
the nohup allows your_command to continue even though the process that launched may terminate first. If it does, the the SIGNUP signal will be sent to your_command causing it to terminate (unless it catches that signal and ignores it).

by using command: nohup MY_COMMAND > /dev/null 2>&1 & echo $! we can return the pid of the process.


This uses wget to notify a URL of something without waiting.

$command = 'wget -qO-';
exec('nohup '.$command.' >> /dev/null 2>&1 echo $!',$pid);

Popular Posts