Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joergpatz/9064e681e875035317c6 to your computer and use it in GitHub Desktop.
Save joergpatz/9064e681e875035317c6 to your computer and use it in GitHub Desktop.
bookmark: fork cli php script from webserver php script
//fork a CLI script from a webserver PHP script using the popen() function
//This allows to nicely transfer parameters to the new script instance like this:
$bgproc = popen('php "/my/path/my-bckgrnd-proc.php"', 'w');
if($bgproc===false){
die('Could not open bgrnd process');
}else{
// send params through stdin pipe to bgrnd process:
$p1 = serialize($param1);
$p2 = serialize($param2);
$p3 = serialize($param3);
fwrite($bgproc, $p1 . "\n" . $p2 . "\n" . $p3 . "\n");
pclose($bgproc);
}
//In the CLI script receive these params like this:
$fp = fopen('php://stdin', 'r');
$param1 = unserialize(fgets($fp));
$param2 = unserialize(fgets($fp));
$param3 = unserialize(fgets($fp));
fclose($fp);
//...and do anything with them that would take to long under webserver control.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment