Check If a Process Is Running or Not on Windows

How to check if a process is running on Windows?

There is no direct way to query general processes as each OS handles them differently.

You kinda stuck with using proxies such as direct OS commands...

You can however, find a specific process using tasklist.exe with /fi parameter.

e.g: tasklist.exe /nh /fi "Imagename eq chrome.exe"
Note the mandatory double quotes.

Syntax & usage are available on MS Technet site.

Same example, filtering for "chrome.exe" in Java:

String findProcess = "chrome.exe";
String filenameFilter = "/nh /fi \"Imagename eq "+findProcess+"\"";
String tasksCmd = System.getenv("windir") +"/system32/tasklist.exe "+filenameFilter;

Process p = Runtime.getRuntime().exec(tasksCmd);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

ArrayList<String> procs = new ArrayList<String>();
String line = null;
while ((line = input.readLine()) != null)
procs.add(line);

input.close();

Boolean processFound = procs.stream().filter(row -> row.indexOf(findProcess) > -1).count() > 0;
// Head-up! If no processes were found - we still get:
// "INFO: No tasks are running which match the specified criteria."

Batch program to to check if process exists

TASKLIST does not set errorlevel.

echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"
exit

should do the job, since ":" should appear in TASKLIST output only if the task is NOT found, hence FIND will set the errorlevel to 0 for not found and 1 for found

Nevertheless,

taskkill /f /im "notepad.exe"

will kill a notepad task if it exists - it can do nothing if no notepad task exists, so you don't really need to test - unless there's something else you want to do...like perhaps

echo off
tasklist /fi "imagename eq notepad.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "notepad.exe"&exit

which would appear to do as you ask - kill the notepad process if it exists, then exit - otherwise continue with the batch

Check if a process is running or not?

Here's another way to do it using your code as a base:

@echo off
Set "MyProcess=calc.exe"
echo "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 (Echo "%MyProcess%" est en cours d^'execution) else (start "" "%MyProcess%")
pause

How to tell if a process is running via batch or script file?

Powershell has an in-built function. Get-Process
Get-Process will tell you about all the processes. If you wish to filter with a particular one then use :

Get-Process|?{$_.Name -eq 'Notepad'}

Screenshots are for reference:

Type Powershell in cmd prompt:

Sample Image

Run the above query. If the notepad is running. It will show you:
Sample Image

Hope it helps...

Check if a process is running or not on Windows?

You can not rely on lock files in Linux or Windows. I would just bite the bullet and iterate through all the running programs. I really do not believe it will be as "expensive" as you think. psutil is an excellent cross-platform python module cable of enumerating all the running programs on a system.

import psutil    
"someProgram" in (p.name() for p in psutil.process_iter())

How do you list all processes on the command line in Windows?

Working with cmd.exe:

tasklist

If you have Powershell:

get-process

Via WMI:

wmic process

(you can query remote machines as well with /node:ComputerOrIP, and there are a LOT more ways to customize this command: link)

check if process is running with only Path provided

You can filter by Path and select Id:

Get-Process | Where-Object {$_.Path -EQ "C:\Users\Administrator\Desktop\1\hello.exe"} | Select-Object Id

By the way:

Get-Process sadly does not even shows you the path to the process.

It does, if you ask it to (it returns an object like all those commands do, and it has a default selection of properties to list but that doesn't mean you can't access the remaining properties by specifically selecting them):

Get-Process explorer | Select-Object Path

(or use Select-Object * to see all available properties)

It appears you haven't yet understood how PowerShell objects work, so I'd recommend you to check out some tutorials about this topic, for instance this article about selecting and filtering and this one that goes more into detail about the filtering options.

Judging if a process exists by pid windows

Would there be a better method on approaching this type of problem?

Indeed so.

Using PID for checking if process is alive is not a solution - so you should go back and check your overall design.

Just use the handle you get when the process is started:

HANDLE hProcess = CreateProcess(...

It could sound like the process is not created from this process - so perhaps obtain it like you do once, and keep it (ie. try to store the handle once the process is found instead of keep using the PID).

Now you can check using for example the function GetExitCodeProcess ala.:

  DWORD returnCode{};
if (GetExitCodeProcess(handle, &returnCode)) {
if (returnCode != STILL_ACTIVE) {
//no longer active

The reason using PID is bad is twofold: the OS may keep the process in a dead state for a while and the PID may already be reused for a new process once you check it (you simply cannot control that in any normal cases).

As for the ERROR_ACCESS_DENIED: it's not a reliable method at all to check if the process exists or not.



Related Topics



Leave a reply



Submit