Power to Build

Home » Operating Systems » DOS » Check if a process is running (Windows)

Check if a process is running (Windows)

While making the script to switch the desktop between 2 versions of PB (see here), I needed a command expression to check if a task (PB and EAServer in this case) is running already. There are many ways to do it on *nix. But on Windows, it’s a bit convoluted. After researching on the net, this is what I came up with.

tasklist /nh /fi "imagename eq notepad.exe" | findstr /i "notepad.exe" >nul && (echo Notepad is running) || (Echo Notepad is not running)

That’s all in one line. If you need to break, use the continuation char, ^.

I use the above command expression in the batch file to check if a process is running already before running it again. I use this is a batch file I created to switch between 2 different versions of PowerBuilder (PB). I wanted to make sure, one version of PB is not running, before letting the user to switch, hence this check. (replace notepad.exe with PB125.exe above).

Details

/nh means no header, /fi means filter. We are filtering for Notepad.exe only above. Then, we take that and look for Notepad.exe using a FindStr command. It will still work, even if we don’t have those 2 flags, but it makes finding the right program quicker.

&& and || are part of the conditional expressions see here. We use these to print for the IF…ELSE condition.

>nul is the equivalent of Unix /dev/null

Rest is self explanatory, I think.

I’ve also posted this scriptlet on the commandfu.com site.

http://www.commandlinefu.com/commands/view/13654/check-if-a-process-is-running-windows


Comments, please?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: