Sometimes in web programming, you might need to run (or fork as they say) processes in the background so the web user doesn’t see all of the processing time plus it can potentially “freeze” the webpage on long data processing features of the site.
For that, you can always fork more processes.
The usual line I use is:
exec ("/usr/bin/php proc.php >/dev/null 2<&1");
The problem I incurred with above line was that processes were forked except there was still some delay that the visitors saw.
To fix the issue so there’s absolutely no delay, I found this great article on how to run background processes, the line that helped is:
exec ("/usr/bin/php proc.php >/dev/null &");
There’s also a section on how to “talk” to the background processes but I won’t get into that here.
Another great way to run processes in the background is to make a “looping” PHP file that will keep forking itself. (or creating forking processes) I actually used this method just now to solve a big headache problem of PHP file taking too long. Since the PHP code was repeating itself only with difference of one variable, I was able to make a new PHP file that would keep forking itself until the processes were done.
The greatest part about this is that the processes run almost in parallel because there’s no delay.
If you want to know more about this, leave a comment and I can make a new blog post about it.
For grabbing values off command line PHP, just use $_SERVER[‘argv’][1] values to grab it.
One Response to PHP Hack – How to Run Background Processes!
Leave a Reply

“2&1”
d’oh.