php 获取windows进程,PHP获取python进程并终止它。Xamp/Windows

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值