Instructables

Check if a process is running from a batch file?

I need a command simmilar to “IF EXIST” to check if a process, such as wmplayer.exe is running. Something like this.

if exist wmplayer.exe (echo proccess running) else (echo proccess not running

Can you think of anything?



**I thought I was close with this, but I can't get it to work.**



IF ”%notepad.exe%”==”” (echo The process is not running) ELSE (echo the program is running)

With it set up like this it always says the process is not running

IF ”%notepad.exe”==”” (echo The process is not running) ELSE (echo the program is running)

With this it aways it running.

Any ideas on how to get it to work? I know I’m really close.

g-one3 years ago

Redirect to NUL after FIND.
Try this
:
@echo off
tasklist /nh /fi "imagename eq wmplayer.exe" | find /i "wmplayer.exe" >nul && (
echo Windows Media Player is running
) || (
echo Windows Media Player is not running
)
pause>nul


XOIIO (author)  g-one3 years ago
Man, you are a genius! THANK YOU SO MUCH! How did you figure it out?
XOIIO (author) 3 years ago
I got this to work.

@echo off
tasklist /FI "IMAGENAME eq wmplayer.exe" | find /I "wmplayer.exe"
IF ERRORLEVEL 2 ECHO Windows Media Player is running
IF ERRORLEVEL 1 ECHO Windows Media Player is not running
pause>nul

But it shows this when the process is running.

wmplayer.exe 13788 Console 1 20,300 K

I tried adding >nul to the tasklist line but then it didn't show anything when the process was running.
Not as tidy as g-one's but you can use for and test for the message "INFO: No tasks are running which match the specified criteria."...

@echo off
for /f "usebackq" %%Z in (`tasklist /nh /fi "imagename eq wmplayer.exe"`) do if %%Z==INFO: goto processnotrunning
echo Windows Media Player is running
exit
:processnotrunning
echo Windows Media Player is not running
Can you use wildcards using this? like to see if ANY executable process is running? lol]

like this:

for /f "usebackq" %%Z in (`tasklist /nh /fi "imagename eq *.exe"`) do if %%Z==INFO:
REM ---Copy all of this text into a notepad and save it as a batch file
REM ---to see if a program is running
@echo off
:start
set /p runningprocess=Enter a program name to see if it is running:
rem ---- 1: all one line ----
tasklist /FI "IMAGENAME eq %runningprocess%" | find /I "%runningprocess%" > nul
rem ---- 1: end of line
IF %ERRORLEVEL% equ 0 ECHO Notepad is running
IF %ERRORLEVEL% equ 1 ECHO Notepad is not running
pause>nul
goto start
Just because I can, here's another answer.

Link.

Up there is a link to a dostips function called IsServiceRunning.

Hope it helps.



Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!