I have a process intensive task that I would like to run in the background.
The user clicks on a page, the PHP script runs, and finally, based on some conditions, if required, then it has to run a shell script, E.G.:
shell_exec('php measurePerformance.php 47 844 [email protected]');
Currently I use shell_exec, but this requires the script to wait for an output. Is there any way to execute the command I want without waiting for it to complete?
PHP exec will wait until the execution of the called program is finished, before processing the next line, unless you use & at the end of the string to run the program in background.
The shell_exec() function is an inbuilt function in PHP that is used to execute the commands via shell and return the complete output as a string. The exec() function is an inbuilt function in PHP that is used to execute an external program and returns the last line of the output.
sh extension. Write the script in the file using an editor. Make the script executable with command chmod +x <fileName>. Run the script using ./<fileName>.
How about adding.
"> /dev/null 2>/dev/null &" shell_exec('php measurePerformance.php 47 844 [email protected] > /dev/null 2>/dev/null &');
Note this also gets rid of the stdio and stderr.
This will execute a command and disconnect from the running process. Of course, it can be any command you want. But for a test, you can create a php file with a sleep(20) command it.
exec("nohup /usr/bin/php -f sleep.php > /dev/null 2>&1 &");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With