【问题标题】:PHP get python processes and kill it. Xamp/WindowsPHP 获取 python 进程并杀死它。 Xamp/Windows
【发布时间】:2016-11-05 17:43:11
【问题描述】:

如何获取所有 python 进程以及每个进程的参数并通过 PHP Xampp/Windows 杀死它

【问题讨论】:

标签: php python windows xampp


【解决方案1】:

在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 ?
  • 是的,如果您的进程 ID 是 1111,那么命令将是:taskkill /PID 1111
【解决方案2】:
<?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;
  }
}

【讨论】:

    【解决方案3】:

    您可以通过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();
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-13
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 2022-06-29
      • 1970-01-01
      • 2015-08-30
      相关资源
      最近更新 更多