Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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?

like image 687
kishan Avatar asked Oct 08 '13 05:10

kishan


2 Answers

Never, ever, give apache sudo permissions!

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

like image 195
Sudipta Chatterjee Avatar answered Oct 04 '22 00:10

Sudipta Chatterjee


Try posix_kill:

bool posix_kill ( int $pid , int $sig )

Send the signal sig to the process with the process identifier pid.

like image 29
Viacheslav aka Lordz Avatar answered Oct 04 '22 00:10

Viacheslav aka Lordz