1

I have a PHP script like this:

foreach ($items as $item) {
    if ($condition) {
        shell_exec("php background.php > /dev/null 2>/dev/null 
        &");
    }
}

The background script takes about 10 seconds and is supposed to be run in parallel every time the if statement is true. The problem is that, although generally it goes just as planned, sometimes background scripts just don't execute completely. Is there any limit to parallel execution like this and if there is, is it a normal behaviour for the server to kill one process to execute another?

UPD: Btw, if I strengthen the condition, reducing the number of simultaneous processes, everything works like a charm.

miha64
  • 77
  • 1
  • 7

1 Answers1

0

You really should not start a background process like that, regardless of it being relatively short process.

Better to use an alternative proposed in here:

The short version is shell_exec('echo /usr/bin/php -q longThing.php | at now'); but the reasons why are a bit long for inclusion here.

PKo
  • 927
  • 5
  • 14
  • Won't this make my loop slower? – miha64 Sep 04 '17 at 11:15
  • It should not affect the "loop speed" compared to what you have. – PKo Sep 04 '17 at 11:20
  • Ok, I've changed it but the problem remains. Sometimes the background script won't complete. Btw, if I strengthen the condition, reducing the number of simultaneous processes, everything works like a charm. – miha64 Sep 04 '17 at 14:05