PHP获取python进程并杀死它。 XAMP /视窗

我怎样才能得到所有的Python进程与每个进程的参数,并通过PHP的Xampp / Windows杀死它

在Windows上有三个系统命令来获取进程:

tasklist ,可能是最简单的(虽然你没有访问的参数)。

get-process ,需要powershell,看不到它的输出我没有powershell

什么应该满足你的需求: wmic process

所以你应该在PHP中使用system()来运行这个命令,以便得到输出,然后解析它,当你得到进程ID时,使用另一个系统命令来杀死它:

 taskkill /PID 99999 #replace 99999 with the process id. 

你可以通过shell_exec函数使用tasklisttaskkill命令。 以下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(); } 
 <?php $list = str_replace(' ','|',shell_exec('tasklist')); $split = explode("\n", $list); $extension = 'py'; foreach ($split as $item) { preg_match_all('#((.*)\.'.$extension.')[\|]+([0-9]+)#',$item, $matches); if($matches[1][0] != '' and $matches[3][0] != ''){ echo $matches[1][0].' '.shell_exec('Taskkill /pid '.$matches[3][0]).PHP_EOL; } }