casbboy — 2011-12-26T17:14:32-05:00 — #1
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
feketegy — 2011-12-27T07:16:10-05:00 — #2
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.
casbboy — 2011-12-27T23:40:14-05:00 — #3
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?
starlion — 2011-12-28T14:02:08-05:00 — #4
only way i can think of is if your server supports pcntl_fork....