John Davidson

linux - Cannot kill a process by a PHP Script running behind nohup

0 comments
Message:


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.



My Answer:
process_name | grep -v grep | awk '{print $2}'');

foreach (explode("\n", $pids) as $pid) {
if (!empty($pid)) {
shell_exec("kill -9 $pid");
}
}

This script should kill all processes with the specified process_name. However, if the PHP script is running in the background using nohup, it may not have the necessary permissions to kill other processes.

To solve this issue, you can try running the PHP script as a superuser or with sudo privileges. You can also check the permissions of the processes you are trying to kill and make sure the PHP script has the necessary permissions to kill them.

Additionally, you can try using the exec() function instead of shell_exec() to see if that helps in killing the processes.

Rate this post

5 of 5 based on 8565 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us