【发布时间】:2016-11-05 17:43:11
【问题描述】:
如何获取所有 python 进程以及每个进程的参数并通过 PHP Xampp/Windows 杀死它
【问题讨论】:
-
这有什么问题? superuser.com/questions/914782/…
如何获取所有 python 进程以及每个进程的参数并通过 PHP Xampp/Windows 杀死它
【问题讨论】:
在windows上有三个系统命令来获取进程:
tasklist,可能是最简单的(尽管您无权访问 args)。
get-process,需要powershell,看不到输出我没有powershell
什么应该满足您的需求:wmic process
所以你应该在 PHP 中使用 system() 运行这个命令,这样你就可以得到输出,然后解析它,当你得到进程 id 时,使用另一个系统命令来杀死它:
taskkill /PID 99999 #replace 99999 with the process id.
【讨论】:
taskkil /PID 1111 #replace 1111 with httpd process id ?
taskkill /PID 1111
<?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;
}
}
【讨论】:
您可以通过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();
}
【讨论】: