Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm currently using the following BAT file to restart a remote service:

sc \\MyServer stop MyRemoteService
sc \\MyServer start MyRemoteService

This works great for one computer, but I want to use it to restart services in a farm. I could just list every server in my batch file, but I only want to restart services if they're already running. For example, if the service is running on one server in the farm, restart it, but if it's not already running, then leave it stopped.

Is there any way to accomplish this remotely? I'm not married to SC is there's another program that does this.

share|improve this question

4 Answers 4

up vote 4 down vote accepted

This is a little more verbose than Izzy's answer, but allows for a longer duration for the service in the "STOP_PENDING" state.

Assume servers in a textfile specified on the command line, of the format:

SERVERNAME1
SERVERNAME2
...

And the script:

@echo off
set SVC=ServiceName
if "%1"=="" goto end

for /f %%i in (%1) do call :do_bounce %%i
goto end

:do_bounce
rem Query for service running and bail if not
sc \\%1 query %SVC% | find "RUNNING"
if errorlevel 1 goto end

rem Stop the service and loop checking for it to stop
sc \\%1 stop %SVC%
:check_stopped
ping 127.0.0.1 -n 2 >NUL 2>NUL
sc \\%1 query %SVC% | find "STOPPED"
if errorlevel 1 goto check_stopped

rem Restart the service
sc \\%1 start %SVC%

:end
share|improve this answer
    
Thanks!! I modified it a bit, but it works like a charm. I have learned a VERY interesting thing here today, about how to make a batch file wait for a second or two and then go on. Thank you very much –  Rihan Meij Oct 27 '10 at 8:07

This is such a dirty answer to the question it cheered me up :)

sc \\MyServer query MyRemoteService | find "RUNNING" && echo Service running - now stopping && sc \\MyServer stop MyRemoteService && ping 127.0.0.1 -n 10 && echo Now starting service && sc \\MyServer start MyRemoteService || echo Service not running
share|improve this answer
2  
you sir are evil :) –  MikeJ Nov 3 '09 at 4:31
2  
The one-liner is very cute, however I'm concerned about the pause waiting for the service to actually stop versus polling to see that it has stopped. It's a cute hack for services that stop quickly and reliably, but I wouldn't trust stopping a service that has a variable time-to-stop. –  Evan Anderson Nov 3 '09 at 4:41
1  
@Izzy - +1 - nice.. –  Critical Skill Nov 3 '09 at 8:01

Powershell 2.0 has a Restart-Service commandlet

share|improve this answer

You might want to check out this question over on StackOverflow.

It mentions why you might want to consider doing the monitoring from each individual machine (with a windows service monitoring tool), versus relying on the network.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.