Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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:


And the script:

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

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

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%
ping -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%

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 -n 10 && echo Now starting service && sc \\MyServer start MyRemoteService || echo Service not running
share|improve this answer
you sir are evil :) – MikeJ Nov 3 '09 at 4:31
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
@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


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.