kishan kishan - 1 year ago 146
Linux Question

How to kill a linux process using pid from php?

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 proc_open() function in the background.
It works fine. But now i want to kill this process using its Process Id.
For that i am firing a command: exec("sudo kill -9 $pid"); where $pid is its process id which i am getting correct from my php code.
The problem is this process is running on behalf of apache user. I thought there may be some
permission issue so I even added apache user to the sudoers file in the following way:
apache ALL=(ALL) NOPASSWD:ALL. But still I am not able to kill it. But the same kill command works from my putty console.
My code is on Amazon EC2 instance.
Any help will be appreciated.
Thanks in advance..

Answer Source

Never, ever, give apache sudo permissions!

Use exec("kill -9 $pid"); - your apache process started it, it can kill it :)