0

I have a php script that perform some actions after killing some old processes.

act.php

$pids = shell_exec('ps aux | grep "saso" | awk \'{print $2}\'');
$pids = str_replace("\n", ' ', $pids);
$pids = array_filter(explode(' ', $pids));

foreach ($pids as $pid) {
    shell_exec('kill -9 ' . $pid . ' > /dev/null 2>&1 &');
}

// reset of the code . ..

The script works well by running php act.php. It fetch process ids, kill it, then run the rest.

But it is not working when I run nohup php act.php & or nohup php act.php. The process is not killed.

I need nohup to run the script in the background with no hang up.

Can't PHP script fetch pids behind nohup ? and are there any alternatives ?

Thanks in advance.

Faid
  • 554
  • 1
  • 5
  • 18

1 Answers1

-1

If you search properly, you can find the result.

Try:

$output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');

Or:

exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
Remzi
  • 9
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 08 '21 at 11:58
  • I see. The idea is: `nohup` cannot get `pids`, It always return empty list, However if I run the script without `nohup` EX: `php script.php` it gets the `pids` well. – Faid Dec 11 '21 at 11:40