Multiprocessing using built-in fork functions

suggest change

These examples start a second PHP process. They do not call pcntl_fork, which exists only on Unix. The parent does not wait for the Unix child and does not hear back from it.

On Windows, pclose(popen(...)) waits until worker.php exits, so that branch is not background work. It can also open a console window. pcntl is not available there.

master.php

$cmd = "php worker.php 10";
if(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') // for windows use popen and pclose
{
    pclose(popen($cmd,"r"));
}
else //for unix systems use shell exec with "&" in the end
{
    exec('bash -c "exec nohup setsid '.$cmd.' > /dev/null 2>&1 &"');
}

worker.php

//send emails, upload files, analyze logs, etc
$sleeptime = $argv[1];
sleep($sleeptime);

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents