views:

31873

answers:

10

How can I check if an application is running from a batch (well cmd) file?

I need to not launch another instance if a program is already running. (I can't change the app to make it single instance only.)

Also the application could be running as any user.

A: 

You should check the parent process name

CodeProject Article - .NET based solution

Non-Programmatic way to check:

  1. Launch Cmd.exe
  2. Launch an app [for instance, c:\windows\notepad.exe]
  3. Check Properties of Notepad.exe process in Process Explorer
  4. Check for parent process.[This shows cmd.exe]

The same can be checked by getting ParentProcess Name.

Prakash
A: 

I'm assuming windows here. So, you'll need to use WMI to get that information. Check out The Scripting Guy's archives for a lot of examples on how to use WMI from a script.

Chris Lively
yes windows. If pos I'd like to do it in DOS, not VBScript.
Matt Lacey
+5  A: 

Or, you could play with the various switches 'tasklist' (on the windows command shell) supports.

ayaz
Thanks for the suggestion. I'd already found the command when you posted this answer. I've created an answer with an example below.
Matt Lacey
+16  A: 

Here's how I've worked it out:

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end

start notepad.exe

:end

del search.log

The above will open notepad if it is not already running

Edit: Note that this won't find apps hidden from the tasklist. This will include any scheduled tasks running as a different user, as these are automatically hidden.

Matt Lacey
This works for me, on XP. Haven't checked anything else.
Matt Lacey
A: 

I don't know how to do so with built in CMD but if you have grep you can try the following:

tasklist /FI "IMAGENAME eq myApp.exe" | grep myApp.exe
if ERRORLEVEL 1 echo "myApp is not running"
Motti
see my answer above, which solves this all from DOS.
Matt Lacey
A: 

I use PV.exe from http://www.teamcti.com/pview/prcview.htm installed in Program Files\PV with a batch file like this:

@echo off
PATH=%PATH%;%PROGRAMFILES%\PV;%PROGRAMFILES%\YourProgram
PV.EXE YourProgram.exe >nul
if ERRORLEVEL 1 goto Process_NotFound
:Process_Found
echo YourProgram is running
goto END
:Process_NotFound
echo YourProgram is not running
YourProgram.exe
goto END
:END
+17  A: 

Another possibility i came up with inspired by using grep is this

tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" echo Programm is running

It doesn't need to save an extra file, so i prefer this method

this worked for me nicely (windows XP SP3). IMHO this is the most elegant way of all proposed here, using just the tools shipped with windows
keftebub
A: 

Thank you! I know these notes are a couple of years old, but GOLD to me. I used the script provided by Matt (10/2/08). The only thing I had trouble with was that it wouldn't delete the search.log. I expect because I had to cd to another location to start my program. I cd'd back to where the bat file and search.log are, but still wouldn't delete. So I resolved that by deleting the search.log first instead of last.

del search.log 

tasklist /FI "IMAGENAME eq myprog.exe" /FO CSV > search.log

FOR /F %%A IN (search.log) DO IF %%-zA EQU 0 GOTO end

cd "C:\Program Files\MyLoc\bin"

myprog.exe myuser mypwd

:end
Nin
This should be a comment.
Sebastian
@Sebastian: There's a little extra information added here, so I'm inclined to leave it as an answer.
Bill the Lizard
A: 

tasklist /FI "IMAGENAME eq notepad.exe" /FO CSV > search.log FOR /F %%A IN (search.log) DO IF %%~zA EQU 0 GOTO end start notepad.exe :end del search.log

Can you do the same thing but in vbscript? thank you

Hiya
A: 

@motti

This works admirably, except use qgrep in windows (resource kit):

tasklist /FI "IMAGENAME eq myApp.exe" | qgrep myApp.exe
if ERRORLEVEL 1 (start myapp.exe) ELSE (goto:EOF)
ghost