2016-07-04 4 views

答えて

4

Windows上の3つのシステムは、プロセスを取得するコマンドがあります。

tasklistは、おそらく最も簡単です(あなたはargsにアクセスできないが)。 wmic process

あなたが得るように、あなたがPHP内system()を使用してこのコマンドを実行する必要があります

get-processは、その出力を見ることができない私はPowerShellの

そして、何あなたのニーズを満たす必要がありますを持っていない、PowerShellを必要とし出力は、その後、それを解析し、あなたがプロセスIDを取得するときに、それを殺すために別のシステムコマンドを使用します。

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

'taskkil/PID 1111#httpdプロセスIDを持つ1111 #replace 11'? – dsgdfg

+0

はい、プロセスIDが1111の場合、コマンドは次のようになります: 'taskkill/PID 1111' –

0
<?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(); 
} 
関連する問題