Php exec command (or similar) so as not to wait for the result

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

<?php echo "Starting Script"; exec('run_baby_run'); echo "Thanks, Script is running in background"; ?> 

Is it possible that PHP did not wait for the result, that is, just release it and go to the next command.

I canโ€™t find anything and am not sure if this is possible. The best I could find was the one who started CRON in a minute.

+48
php exec
Sep 29 '10 at 6:52
source share
4 answers

It is always useful to check the documentation :

To execute the command, do not damage your php script while it is running, the program you are running should not output back to php. To do this, redirect both stdout and stderr to / dev / null, and then run it.

> /dev/null 2>&1 &

To execute the command and it appeared as another process that does not depend on the apache thread to continue to work (it will not die if someone cancels the page):

exec('bash -c "exec nohup setsid your_command > /dev/null 2>&1 &"');

+78
Sep 29 '10 at 6:57
source share
โ€” -

You can run the command in the background by adding & at the end of it as:

 exec('run_baby_run &'); 

But only this one will hang your script, because:

If the program is launched using the exec function, the program output must be redirected to a file or other output stream to continue working in the background. Otherwise, PHP will hang until the program terminates.

So, you can redirect the stdout command to a file if you want to see it later or /dev/null if you want to cancel it as:

 exec('run_baby_run > /dev/null &'); 
+38
Sep 29 '10 at 6:54
source share

Well, here is the final final right award answer; -)

This uses wget to notify the URL of something without expecting it.

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

This uses ls to update the log without waiting.

 $command = 'ls -la > content.log'; exec('nohup ' . $command . ' >> /dev/null 2>&1 & echo $!', $pid); 
+6
Aug 02 '14 at 10:17
source share

"exec nohup setid your_command"

nohup allows your team to continue working, even if the running process may be the first to terminate. If so, the SIGNUP signal will be sent to the your_command command, which will lead to its completion (unless it catches this signal and pays attention to it).

0
Mar 06 '14 at 19:44
source share



All Articles