How to kill linux process with pid from php?

I ran into one problem related to killing a Linux process from my php code. I am running the Scrapy tool from my php code using a function proc_open()in the background.

It works fine, but now I want to kill this process using its process id. For this, I use exec("sudo kill -9 $pid");, where $pidis the identifier of the process that I get from my php code.

The problem is that this process is running on behalf of the apache user. I thought there might be an issue with permissions, so I added the apache user to the sudoers file, like this one apache ALL=(ALL) NOPASSWD:ALL, but I still can't kill him. One way or another, the same kill command works from my putty console.

My code is on an Amazon EC2 instance.

My question is: how can I kill this process identified by pid from php?

+4
source share
3 answers

Never grant apache permissions sudo!

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

+17
source

You can use exec with sudo privileges for a skill that safer checks to kill user processes on Linux with php

Thanks and Regards,
Alok Thaker

0
source

posix_kill:

bool posix_kill ( int $pid , int $sig )

sig ​​ pid.

0

All Articles