A batchfile to check the status of Windows services and start them automatically if they have stopped

I deal with several applications on a day to day basis, and I was having problems with one in particular it’s associated windows services stopping for no tangible reason.

I therefore devised a batchfile to check the status of each of the services in turn, and start them if they stopped.

@echo off
:start
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Control Manager is running @for /f "tokens=3" %%a in ('sc query "KmpCM"^|find "STATE"') do (
@if %%a==4 goto runningCM
@goto stoppedCM
)
:stoppedCM
net start KmpCM
:runningCM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Extension Manager is running @for /f "tokens=3" %%a in ('sc query "KmpEM"^|find "STATE"') do (
@if %%a==4 goto runningEM
@goto stoppedEM
)
:stoppedEM
net start KmpEM
:runningEM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Message Server is running @for /f "tokens=3" %%a in ('sc query "KmpMS"^|find "STATE"') do (
@if %%a==4 goto runningMS
@goto stoppedMS
)
:stoppedMS
net start KmpMS
:runningMS
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Offline Manager is running @for /f "tokens=3" %%a in ('sc query "KmpOM"^|find "STATE"') do (
@if %%a==4 goto runningOM
@goto stoppedOM
)
:stoppedOM
net start KmpOM
:runningOM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Permission Manager is running @for /f "tokens=3" %%a in ('sc query "KmpPM"^|find "STATE"') do (
@if %%a==4 goto runningPM
@goto stoppedPM
)
:stoppedPM
net start KmpPM
:runningPM
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Scheduler is running
@for /f "tokens=3" %%a in ('sc query "KmpSH"^|find "STATE"') do (
@if %%a==4 goto runningSH
@goto stoppedSH
)
:stoppedSH
net start KmpSH
:runningSH
@echo OK.
@echo.
@ping 127.0.0.1 -n 60 -w 1000 > nul
@echo Checking if Service Manager is running @for /f "tokens=3" %%a in ('sc query "KmpSM"^|find "STATE"') do (
@if %%a==4 goto runningSM
@goto stoppedSM
)
:stoppedSM
net start KmpSM
:runningSM
@echo OK.
@echo.
@goto start

Leave a comment