I’m trying to use popen to run a php script in the background. However, I need to pass a (fairly large) serialized object.
$cmd = "php background_test.php >log/output.log &";
$fh = popen($cmd, 'w');
fwrite($fh, $data);
fclose($fh);
//pclose($fh);
Without the ampersand this code executes fine but the parent script will wait until the child is finished running. With the ampersand STDIN gets no data.
Any ideas?
As far as I know there is no way in php to send a process in background and continue to feed its STDIN (but maybe I’m wrong). You have two other choices here:
background_test.phpto get its input from command line and transform your command line inphp background_test.php arg1 arg2 ... >log/output.log &background_test.phpscript with that file as in the following codeExample for point 2: