I have a batch script that looks like:

sc stop myservice
sc start myservice

it errors out because sc doesn't wait till the service is stopped. How do I restart a service with a script?

link|improve this question

feedback

8 Answers

up vote 23 down vote accepted

The poster wants to ensure the service is stopped before trying to restart it. You can use a loop on the output of "sc query" doing something like this:

:stop
sc stop myservice

rem cause a ~10 second sleep before checking the service state
ping 127.0.0.1 -n 10 -w 1000 > nul

sc query myservice | find /I "STATE" | find "STOPPED"
if errorlevel 1 goto :start
goto stop

:start
net start | find /i "My Service">nul && goto :loop
sc start myservice
link|improve this answer
5  
Nice use of ping as a time delay. – David Yu Jun 12 '09 at 23:34
3  
elegant code, nice one! – Greg Meehan Jun 12 '09 at 23:35
Thanks, there's no sleep in batch so that's all you can do to wait :) – crb Jun 12 '09 at 23:37
4  
+1, and a GOTO badge. Awarded to those that use a GOTO statement with a straight face. – Joseph Kern Jun 13 '09 at 1:20
If only batch supported do/while loops... It's trivial in C#, really! – crb Jun 13 '09 at 1:37
show 7 more comments
feedback

May be missing something, but I use this all the time:

net stop "myservice"
net start "myservice"

or shorter:

net stop "myservice" && net start "myservice"

link|improve this answer
feedback

Dead simple with powershell:

PS >Restart-Service MySrvcHere

Even better, using display names:

PS >Restart-Service -displayname "My Service Name Here"

Get-Help Restart-Service for more

link|improve this answer
feedback

what about this?

link|improve this answer
feedback

If it is purely for restarting the service, you can use

Net stop myservice
Net start myservice

However, if you want access to the options of sc, you can use the start /wait command

start /B /WAIT CMD /C "sc stop myservice"
start /B /WAIT CMD /C "sc start myservice"

this technique is a more general solution that can be applied to any command.

link|improve this answer
feedback

If you want to restart a failed service you do not need to run a script. In the services MMC snapin right click on a service, select properties, click the recovery tab. Here you can set what actions you want taken should the service stop. There is alot of flexibility available. You will need a script if y ou are trying to stop the service , do something then start the script, preface the batch file with net stop "myserviceshortname" and end with net start "myserviceshortname"

In vbscipt it's a little more code to stop a service and its' dependants:

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='myservice'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Antecedent" )
For each objService in colServiceList
    objService.StopService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery _
        ("Select * from Win32_Service where Name='myservice'")
For each objService in colServiceList
    errReturn = objService.StopService()
Next

Here's starting a service and anything it depends on (this should be familiar)

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
    ("Select * from Win32_Service where Name='Myservice'")
For each objService in colServiceList
    errReturn = objService.StartService()
Next
Wscript.Sleep 20000
Set colServiceList = objWMIService.ExecQuery("Associators of " _
   & "{Win32_Service.Name='myservice'} Where " _
        & "AssocClass=Win32_DependentService " & "Role=Dependent" )
For each objService in colServiceList
    objService.StartService()
Next
link|improve this answer
feedback

If you want to stop service in the remote machine, follow the instruction

Step 1: login to remote machine with know credential like administrator

Step 2: Open command prompt in your computer

Step 3: sc \Remote_Machine_IPAddress stop "Service Name"

Best wishes

link|improve this answer
feedback

To have quiet restart of some service, which asks confirmations to be stopped (as Server service, for example), You could add /y to the end of stop command.

net stop Server /y
net start Server

It would be helpful for automatic script execution.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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