Php check if process is running windows

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");
  }
}

In This Article

  • 1 Introduction
  • 2 Complete Code to Check Process Running in Windows Using PHP
    • 2.1 Code Explanation:
    • 2.2 Output:
  • 3 How to Kill Windows Process Using PHP
    • 3.1 Code Explanation:
  • 4 Also Read:
    • 4.1 Related

Introduction

To Check Process Running in Windows using PHP, we have to use PHP tasklist command with exec() function.

Php check if process is running windows

<?php

// show tasks, redirect errors to NUL (hide errors)

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

echo"<pre>";

print_r($task_list);

?>

Code Explanation:

  • On the above code, we use the PHP tasklist command to see what processes are running in Windows.
  • exec is the PHP inbuilt function used to execute an external program and return the last value, if there is no return then it returns the NULL value.
  • In the last line we print the $task_list variable where we store the list of running processes in windows.

Output:

Php check if process is running windows

You can check it by running it on you local machine using XAMP or WAMPP.

Also Read: Best PHP IDE Code Editor in 2021 [Updated]

How to Kill Windows Process Using PHP

<?php

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

?>

Code Explanation:

On the above code, we use the same exec() function to execute the PHP tasklist command and by using taskName from the list we can kill the process.

Here is the complete explanation about how we can check process running in windows and also kill by using the command.

To know more you can check PHP: getmypid – Manual.

Also Read:

  • Set PHP Error Reporting Into The PHP File
  • How to Get Current Date and Time In JavaScript
  • How to Get Input Value From User in PHP?
  • Complete Guide PHP Error Log

Happy Coding..!

Was this article helpful?

YesNo

Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.