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.
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.
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'.
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");
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/