5

I need to know and kill if there is any processes running a specified PHP script. Is that possible to get list of processes running sample.php using exec() and a php script.

4 Answers 4

16
exec("ps auxwww|grep sample.php|grep -v grep", $output);

This would only work, though, if PHP is running in CGI mode. If it's running as a SAPI type thing, you'll never see "sample.php" in the process list, just 'httpd'.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you it works, but what are the numbers shown here? 1025 19622 0.0 0.0 5336 1308 ? S 02:15 0:00 wget -q http://www.example.com/sample.php
parent process id, process ID, cpu usage, memory usage, etc... look at man ps for details.
Unfortunately, I'm getting Error, do this: mount -t proc proc /proc.
0

There isn't. Because PHP is ran through apache/nginx. In case of command-line-access, proccess is named PHP, not actual name of your script.

Comments

0

It depends on lots of factors including OS, PHP version, etc., but you could try using signals to get a script to give you its name and then terminate if it matches. Or, have the script register its pid and then compare with running processes.

http://stuporglue.org/handling-signals-in-php/

Comments

0

this helped me kill rogue processes via a url parameter. i figured i'd contribute to the discussion in case someone else is still poking around for answers.

load yikes.php. identify the process id (it should be the first integer you come to in each index of the array). copy and paste it into the url as ?pid=XXXXX. and it's gone.

//http://website.com/yikes.php?pid=668
$pid = $_GET['pid'];
exec("ps auxwww|grep name-of-file.php|grep -v grep", $output);
echo '<pre>';
print_r($output);
echo '</pre>';
//
exec("kill $pid");

Comments