Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to check if a system service is running

I would like to know how to check if a service is running using a batch file

e.g.

if xxxx service is running go to start stage2.bat else go to echo Service not running

Any help would be appreciated

Thanks

like image 253
James Avatar asked May 09 '11 17:05

James


2 Answers

Similar to How to check if a process is running via a batch script

EDIT:
From the post, with an added else statement:

tasklist /FI "IMAGENAME eq myapp.exe" 2>NUL | find /I /N "myapp.exe">NUL
if "%ERRORLEVEL%"=="0" (
    call stage2.bat
) else (
    echo Program is not running
)

For a service:

sc query "ServiceName" | find "RUNNING"
if "%ERRORLEVEL%"=="0" (
    call stage2.bat
) else (
    echo Program is not running
)
like image 57
John Leehey Avatar answered Oct 16 '22 10:10

John Leehey


read this article http://support.microsoft.com/kb/251192 and see SC /?

then try

SC QUERY

EDIT: to automate the check, pipe the result to FIND and look for RUNNING

 SC QUERY %1 | FIND "STATE" | FIND "RUNNING" >nul
 IF ERRORLEVEL 1 (echo NOT RUNNING ) ELSE (echo RUNNING) 
like image 20
PA. Avatar answered Oct 16 '22 09:10

PA.