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.