Fork a Process in PHP

The other day I ran into a need to “fork” a process in a web app. Basically I needed to trigger a long-running process but immediately show the user a screen that said “running”. I used a version of the following.

function forkExec($command) {
if(stripos(PHP_OS, 'win') !== false) {
$command = "start /b $command";
else {
$command .= ' > /dev/null &';
return pclose(popen($command, 'r'));

In windows `start /b` starts the given command in the background. On Linux, `> /dev/null` supresses output and `&` runs the given command in the background.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Powered by sweet Captcha