Detecting if a Windows process AND application is running

11,593

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");
  }
}
Share:
11,593
drschultz
Author by

drschultz

Updated on June 05, 2022

Comments

  • drschultz
    drschultz almost 2 years

    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?

  • Cito
    Cito over 11 years
    Dunno, if you can distinguish in some way, if its in the taskbar - maybe with some 3rd party tool. But you could give the task some name and match exactly that task in your taskkill if active (tasklist).
  • Dincă Alexandru
    Dincă Alexandru over 9 years
    Great answer! I was wondering if I can do this check on a different server than the one wamp is running on?
  • user
    user almost 3 years
    In case it is not working for you, add start /B, i.e., $task_list = array(); exec("start /B tasklist 2>NUL", $task_list);. Source: taskkill from PHP exec