Batch script to check if service is running
Its been long I was searching for a batch script which checks recursively whether a service is in RUNNING state or not from a remote server?
Its easy to do in a shell script on any UNIX environment but its usually hard to do it in Windows Env.
Here is the sample script which I developed for Windows env. but it requires WMIC installed in the system.
Check whether service is RUNNING or not in remote server
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: set servername=server1 set svcname=servicename2 :CheckStateStart Set STATE=CLOSE for /f "tokens=1" %%i in ('wmic /node:%servername% service where Name^="%svcname%" get state^| find "Running"') do ( rem echo Port:%PORT% on %servername% OPEN! set STATE=OPEN Goto :EOF ) echo Waiting for the Service %svcname% on %servername% to Start... Goto :EOF :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :WaitStart :: %1 is the Remote Machine Name :: %2 is null if attempting to start, is Re- if restarting Set loop=0 :CheckLoopStart :: Will check state 20 times, adjust as needed. If %loop%==240 Goto NoStart :: Wait 5 second for service to (Re-)start (can also use sleep utility) :: adjust -w as needed units are milliseconds :: echo Checking Status Again in 5Seconds. Ping 1.0.0.0 -n 1 -w 5000 >Nul Call :CheckStateStart If NOT "%STATE%"=="OPEN" (set /a loop+=1) & Goto CheckLoopStart Echo Windows Service %svcname% on %servername% is in Running state. ECHO. GOTO :EOF :NoStart Echo Unable to start service %svcname% on %servername% in 20 minutes ECHO Please check the state of service manually. ECHO. Set ErrFlag=1 Goto :EOF :EOF
This is the function which checks for the servicename which is RUNNING or not in the machine.
Check whether service is STOPPED or not in remote server
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :CheckStateStop Set STATE=OPEN for /f "tokens=1" %%i in ('wmic /node:%servername% service where Name^="%svcname%" get state^| find "Stopped"') do ( rem echo Port:%PORT% on %servername% CLOSE! set STATE=CLOSE Goto :EOF ) echo Waiting for the Service %svcname% on %servername% to Stop... Goto :EOF :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: :WaitStop :: %1 is the Remote Machine Name Set loop=0 :CheckLoopStop :: Will check state 20 times, adjust as needed. If %loop%==240 Goto NoStop :: Wait 5 second for service to (Re-)start (can also use sleep utility) :: adjust -w as needed units are milliseconds :: echo Checking Status Again in 5Seconds. Ping 1.0.0.0 -n 1 -w 5000 >Nul Call :CheckStateStop If NOT "%STATE%"=="CLOSE" (set /a loop+=1) & Goto CheckLoopStop Echo Windows Service %svcname% on %servername% is in Stopped state. ECHO. GOTO :EOF :NoStop Echo Unable to stop %svcname% on %servername% in 20 minutes ECHO Please check the state of service manually. ECHO. Set ErrFlag=1 Goto :EOF
Replace the servername with the name of the machine which you want to check and svcname
with the actual service name.
In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.