您可以通过shell_exec函数使用tasklist和taskkill命令。下面的Task演示了如何使用它们来查找有关任务的信息以及如何杀死它们。在class Task {
function __construct($header,$row) {
$this->imageName = $this->findValue($header,$row,'Image Name');
$this->processID = $this->findValue($header,$row,'PID');
$this->commandLine = $this->findValue($header,$row,'Window Title');
}
function findValue($header,$row, $key , $default = '') {
$kk = array_search($key, $header);
return $key !== -1 ? $row[$kk] : $default;
}
public $imageName = '';
public $processID = '';
public $commandLine = '';
public function kill(){
shell_exec( sprintf('taskkill /PID %s',$this->processID));
}
public static function findTask($imageName) {
$csv = shell_exec(sprintf( 'tasklist /FO CSV /V /FI "IMAGENAME eq %1$s"',$imageName));
$lines = explode("\n",$csv);
array_pop($lines);
if ( count($lines) <= 1 ) {
return array();
}
$data = array_map('str_getcsv', $lines);
$tasks = array();
$header = $data[0];
for( $kk = 1 ; $kk < count($data); $kk++ ) {
$row = $data[$kk];
if ( count($row) === count($header) ) {
array_push($tasks, new Task($header, $row));
}
}
return $tasks;
}
}
foreach( Task::findTask('python.exe') as $task ) {
echo sprintf("%s %s %s\n", $task->imageName , $task->processID, $task->commandLine);
$task->kill();
}