Restarting a service with a batch file, and keep checking that it has stopped

Place this in a .bat of .cmd file

@echo off

if "%1"=="" goto instructions

 

if "%3"=="Start" goto start_Service

 

if "%3"=="Stop" goto do_bounce

 

:do_bounce

rem Query for service running and bail if not

sc \\%1 query %2 | find "RUNNING"

if errorlevel 1 goto end

rem Stop the service and loop checking for it to stop

sc \\%1 stop %2

goto check_stopped

 

:check_stopped

ping 127.0.0.1 -n 2 >NUL 2>NUL

sc \\%1 query %2 | find "STOPPED"

if errorlevel 1 goto check_stopped

GOTO END

 

:start_Service

rem Restart the service

sc \\%1 start %2

goto end

 

:instructions

ECHO "Usage is service.bat <server name> <service name> [Start:Stop]"

ECHO "YES it is case sensitive"

 

:end

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.