Location: PHPKode > scripts > Process Killer > process-killer/processkiller.class.php
<?php

/**
 * processkiller
 * 
 * @package GONX
 * @author Hatem Ben Yacoub <hide@address.com>
 * @copyright Copyright (c) 2004-2006
 * @website http://phpmagazine.net
 * @version $Id$
 * @access public
 **/
class processkiller{
	/**
     * Constructor
     * @access protected
     */
	function processkiller(){
		
	}
	
	/**
	 * 
	 * @access private
	 * @var boolean
	 **/
    var $_debug = false;
	
	/**
	 * 
	 * @access public
	 * @return boolean 
	 **/
    function Getdebug(){
		return $this->_debug;
	}
	
	/**
	 * 
	 * @access public
	 * @return void
	 **/
	function Setdebug($newValue){
		$this->_debug = $newValue;
	}

	function run(){
		$argv = $GLOBALS[HTTP_SERVER_VARS][argv];
		$argc = $GLOBALS[HTTP_SERVER_VARS][argc];
		

		if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>

Kill all processes with the same command name

  Usage :
  <?php echo $argv[0]; ?> <command_name>
  
  Sample (kill all instance of php):
 <?php echo $argv[0]; ?> php

  <command_name> Command name is the latest value shown 
  when you run "ps -aux"
  
  Options --help, -help, -h,
  and -?, Will return this help
  
<?php
		} else {
			$debug = true;
		    $tokil = $argv[1];
			
			if (ereg("'",$tokil)){
				$tokil = trim(str_replace("'","",$tokil));
			}
			
			$s = shell_exec("ps -aux");
			$data = explode("\n",$s);
			foreach($data as $k=>$v){
				$v = preg_replace( "/ +/", " ", $v );
				$l = explode(" ",$v);
				$running = array();
				for($z=10;$z<=count($l);$z++) {
						$running[] = $l[$z];
				}
				
				$running = implode(' ',$running);
				$commands[trim($running)][] = $l[1];
			}
			
			$keys = array_keys($commands);

			if (!in_array($tokil,$keys )) {
				if ($this->Getdebug()) {
				    echo "No process $tokil to kill.";
				}
			    
			} else {
				foreach($commands[$tokil] as $v){
					shell_exec("kill -9 $v");
				}
				if ($this->Getdebug()) {
				    echo sizeof($commands[$tokil])." process related to $tokil is killed.";
				}
			}
		}
	}
}

?>
Return current item: Process Killer