Launch External PHP Script with shell_exec?

I’m thinking of using shell_exec to launch an external php script (that talks with a remote mysql database) so the script in that external PHP file does not hold up my page from loading.

Does this work? Does the PHP run shell_exec and just move on with the rest of the page/coding? Or does it wait for whatever external script is run to finish?

And, if it works like I hope, does running shell_exec take more resources from the server than just running the script in the page?

All info appreciated

shell_exec waits for the output to be returned, therefore waits for the command to finish execution.

If you do not want that, then simply discard the output by adding the following to the end of your command (inside shell_exec): > /dev/null 2>/dev/null &


shell_exec ('php external-file.php > /dev/null 2>/dev/null &');

This will start running the external-file.php and forgets about it, because it doesn’t expect back any output from the script.

That is awesome, thanks. Now, is there any disadvantage of using shell_exec over just running the script in the same page? The shell_exec script will likely be run over one-million times per day, so need to be sure. Also, you know of any way of having PHP not wait for a command to execute (finish) without doing it this way?


only way i can think of is if your server supports pcntl_fork…