Voting

: six plus zero?
(Example: nine)

The Note You're Voting On

php [AT] jsomers [DOT] be
19 years ago
<?php

/**
* PHP Kill Process
*
* Sometimes, it can happen a script keeps running when it shouldn't, and it
* won't stop after we close the browser, or shutdown the computer. Because it's
* not always easy to use SSH there's a workaround.
*
* @author Jensen Somers <[email protected]>
* @version 1.0
*/

class KillAllProcesses {
/**
* Construct the class
*/
function killallprocesses() {
$this->listItems();
}

/**
* List all the items
*/
function listItems() {
/*
* PS Unix command to report process status
* -x Select processes without controlling ttys
*
* Output will look like:
* 16479 pts/13 S 0:00 -bash
* 21944 pts/13 R 0:00 ps -x
*
*/
$output = shell_exec('ps -x');

$this->output($output);

// Put each individual line into an array
$array = explode("\n", $output);

$this->doKill($array);
}

/**
* Print the process list
* @param string $output
*/
function output($output) {
print
"<pre>".$output."</pre>";
}

/**
* Kill all the processes
* It should be possible to filter in this, but I won't do it now.
* @param array $array
*/
function doKill($array) {
/*
* Because the first line of our $output will look like
* PID TTY STAT TIME COMMAND
* we'll skip this one.
*/
for ($i = 1; $i < count($array); $i++) {
$id = substr($array[$i], 0, strpos($array[$i], ' ?'));
shell_exec('kill '.$id);
}
}
}

new
KillAllProcesses();

?>

It's not the very best solution, but I've used it a couple of times when I needed to do it quick without to much trouble.
Make not I kill all the processes, on my server px -x will only return like 4 times /sbin/apache and it's pretty safe to kill them without any trouble.

<< Back to user notes page

To Top