[UPHPU] Forking A Script

Ken Snyder ksnyder at coremr.com
Mon Jul 23 12:32:15 MDT 2007


John David Anderson wrote:
> ...
> You may not need to fork. Try appending an ampersand to the end of the 
> command:
>
> $ php doReportScript.php &
>
> That'll run the process in the background (that way PHP won't wait for 
> a return).
>
> -- John
I'm trying this out, but can't get it to work.  I've got two files:

fork.php:
<?php
$path = '/home/ksnyder/www/test/sleep.php';
exec("php $path &");
echo "<p>Called script $path and didn't wait for return value</p>";
?>

sleep.php:
<?php
sleep(10);
file_put_contents(
  '/home/ksnyder/www/test/sleep.log',
  date('Y-m-d H:i:s') . ' ' .print_r($argv,true)
);
?>

 From the command line I call "sudo -u apache php
/home/ksnyder/www/test/sleep.php &" and the shell immediately returns
and the test file is written 10 seconds later (cool!).  But when I go to
a browser to hit fork.php, the script takes 10 seconds, writes to my
test file, _then_ echos the html.  Am I missing something?

Also, is there a similar way to do this on Windows?

-- Ken





More information about the UPHPU mailing list