4

I made this script to test the execution of PHP as a background process

foreach($tests as $test) { 
   exec("php test.php ".$test["id"]); 
} 

as suggested in php process background and How to add large number of event notification reminder via Google Calendar API using PHP? and php execute a background process

But the script does not run faster than when it was all in one script without the addition of test.php.

What am I doing wrong?

Thanks in advance!

Community
  • 1
  • 1
albertopriore
  • 614
  • 2
  • 9
  • 36
  • Similar to a question I answered a few days ago: http://stackoverflow.com/questions/4626860/how-can-i-run-a-php-script-in-the-background-after-a-form-is-submitted/4628279#4628279 – Michael Irigoyen Jan 10 '11 at 16:58

1 Answers1

8

exec() will block until the process you're exec'ing has completed - in otherwords, you're basically running your 'test.php' as a subroutine. At bare minimum you need to add a & to the command line arguments, which would put that exec()'d process into the background:

exec("php test.php {$test['id']} &");
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • does not work the content of $test['id'] is only a number. so the command looks like exec('php test.php &150'); is it correct? – albertopriore Jan 10 '11 at 15:04
  • Woops, sorry, should be `php test.php 150 &`. The ampersand has to be the last character. I'll fix up the answer. – Marc B Jan 10 '11 at 16:48
  • 2
    According to the [manual](http://php.net/manual/en/function.exec.php), it also requires output redirection in order to run in the background. `php test.php 150 > /dev/null &` – jpschroeder May 16 '15 at 15:43