How to kill a linux process using pid from php?
Solution 1
Never, ever, give apache sudo
permissions!
Use exec("kill -9 $pid");
- your apache process started it, it can kill it :)
Solution 2
Try posix_kill
:
bool posix_kill ( int $pid , int $sig )
Send the signal sig to the process with the process identifier pid.
kishan
I am a Software Developer at Niyuj Enterprise Software Solutions, Pune. My area of work is developing Web Apps in PHP, Javascript, HTML and Windows Forms Apps in C# .Net and VB .Net.
Updated on July 05, 2022Comments
-
kishan almost 2 years
I am facing one issue regarding killing a Linux process from my php code. I am running a Scrapy tool from my php code using the
proc_open()
function in the background.It works fine, but now I want to kill this process using its process id. To do that I'm using
exec("sudo kill -9 $pid");
where$pid
is the process id which I'm getting from my php code.The problem is this process is running on behalf of the apache user. I thought there might be some permissions issue, so I added apache user to the sudoers file like this
apache ALL=(ALL) NOPASSWD:ALL
but I'm still not able to kill it. Somehow, the same kill command works from my putty console.My code is on an Amazon EC2 instance.
My question is, how can I kill that process identified by a pid from php?
-
MarcoZen about 10 yearsskill is obsolete? pkill is something you should look at.
-
virusivv over 8 yearsi couldn't do it with exec so i just needed to use shell_exec but anyway thanks it helped me a lot :)
-
Brian C over 4 yearsDon't use
exec()
for this, useposix_kill()
instead. Also, it's bad practice to routinely use kill -9 as it forces the process to die instantly without cleanup (see other answer). Instead, reserve kill -9 for when a process won't die. with kill -15 or kill -1 (SIGHUP).