Why shell_exec executing more than 1 process?

2019-08-22 04:09发布

问题:

I dont understand why there is more than 1 process when I run run.php once from a browser

In the PHP code, I have the following:

run.php

<?php
shell_exec("php theprocess.php > /dev/null 2>&1 &");
?>

theprocess.php

<?php
$z = 1;
while ($z <= 20) {
    echo $z . "\n";
    $z++;
    sleep(3);
}
?>

I execute run.php from the browser (eg: http://localhost/run.php)

Then I typed: ps aux | grep php

username@ [~]# ps aux | grep php
username 27272  0.0  1.5  89504 64468 ?        R    17:33   0:00 php theprocess.php
username 27274  0.0  1.2  89504 49872 ?        R    17:33   0:00 php theprocess.php
username 27276  0.0  0.6  89504 28676 ?        R    17:33   0:00 php theprocess.php
username 27278  0.0  0.0  22280  3704 ?        R    17:33   0:00 php theprocess.php
username 27280  0.0  0.0   1940   508 ?        S+   17:33   0:00 grep php

I dont understand why is it showing more than 1 theprocess.php process?

Also why it still running at the background? it should terminate theprocess.php finish the task. How can that be done?

回答1:

I have fixed the problem!

When running script from a webpage, it does not treat as PHP cli.

Replace

shell_exec("/usr/bin/php theprocess.php > /dev/null 2>&1 &");

To

shell_exec("/usr/bin/php-cli theprocess.php > /dev/null 2>&1 &");

I no longer have multiple procress running in the background.