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开发者_StackOverflow中文版 appreciated
Thanks
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
)
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)
@echo off
color 1F
@sc query >%COMPUTERNAME%_START.TXT
ECHO REPORT MISSING INSTALL SERVICES >%COMPUTERNAME%_MISSING.TXT
find /I "AcPrfMgrSvc" %COMPUTERNAME%_START.TXT >nul
IF ERRORLEVEL 1 NET START "AcPrfMgrSvc"
IF ERRORLEVEL 1 ECHO AcPrfMgrSvc >>%COMPUTERNAME%_MISSING.TXT
My solution, because under Windows7 just IF ERRORLEVEL 1
does not work and errorlevel is 0 in case findstr
successful or not.
In my case, I'm looking for something started by java.exe, lets say HELLO.jar [parameter of java.exe]
wmic PROCESS LIST FULL | findstr /I java.exe | findstr /I HELLO.jar
if ErrorLevel 1 (
Echo OK
msg "%username%" HELLO.jar not started
Pause
) else (
Echo ERR
msg "%username%" HELLO.jar already running
Pause
exit
)
First of all you might need admin privileges and non of the examples deals with that. If you don't make us of nircmd already you could start now
精彩评论