Detecting if a Windows process AND application is

2019-07-15 12:32发布

问题:

I'm investigating if there is a way to programatically check if a certain process is running as a process (in the list of running exe's) AND as an open application (i.e on the taskbar) and take action based on the results.

Also - is there a way to programatically kill a process OR a running application?

We are running a WAMP application on this server so ideally i'd like a way to do this using PHP, but am open to whatever will work best.

Any advice?

回答1:

check if a certain process is running as a process

If you have the tasklist command, sure:

// show tasks, redirect errors to NUL (hide errors)
exec("tasklist 2>NUL", $task_list);

print_r($task_list);

Then you can kill it, using by matching/extracting the tasknames from the lines.

exec("taskkill /F /IM killme.exe 2>NUL");

I used that a lot with php-cli. Example:

// kill tasks matching
$kill_pattern = '~(helpctr|jqs|javaw?|iexplore|acrord32)\.exe~i';

// get tasklist
$task_list = array();

exec("tasklist 2>NUL", $task_list);

foreach ($task_list AS $task_line)
{
  if (preg_match($kill_pattern, $task_line, $out))
  {
    echo "=> Detected: ".$out[1]."\n   Sending term signal!\n";
    exec("taskkill /F /IM ".$out[1].".exe 2>NUL");
  }
}