42

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?

splattne
  • 28,508
  • 20
  • 98
  • 148
Joshua
  • 779
  • 1
  • 9
  • 19

8 Answers8

53

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 :stop
goto :start

:start
net start | find /i "My Service">nul && goto :start
sc start myservice
davidgro
  • 103
  • 2
crb
  • 7,998
  • 1
  • 38
  • 53
  • 11
    Nice use of ping as a time delay. – David Yu Jun 12 '09 at 23:34
  • 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 01:20
  • If only batch supported do/while loops... It's trivial in C#, really! – crb Jun 13 '09 at 01:37
  • I'd change the `sc query myservice | find "STOPPED"` to `sc query myservice| find /I "STATE" | FIND "STOPPED"' to make sure you're getting the STATE line. – Christopher_G_Lewis Jul 31 '09 at 17:09
  • @crb +1 We use almost identical code as part of install script. There is a `sleep` for Windows though, from the Resource Kit. http://ss64.com/nt/sleep.html – jscott Jul 02 '10 at 11:22
  • If you're prepared to install the Resource Kit, you could install PowerShell. :) – crb Jul 05 '10 at 10:20
  • I believe FIND returns 0 if the string was found. Shouldn't you be testing for 0 instead of 1 in this case? – Lars A. Brekken Mar 15 '11 at 20:32
  • @Lars: correct, but if we don't find "stopped", we loop back to stop (the next goto) - if do find "stopped" then it's time to start it! – crb Mar 17 '11 at 20:23
  • So if it's stopped, sc query myservice | find /I "STATE" | find "STOPPED" returns 0, the if errorlevel 1 returns false and you go to :stop to stop it again? I must be missing something here. – Lars A. Brekken Mar 18 '11 at 15:48
  • 3
    In Windows Server 2008, the error level is **0** when found and **1** when not found. So I had to invert the logic and then it worked. – Kirk Woll Jul 11 '11 at 15:42
31

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"

Skawt
  • 698
  • 4
  • 9
25

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

Factor Mystic
  • 463
  • 1
  • 10
  • 15
6

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.

Peter Stuer
  • 1,473
  • 9
  • 11
5

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.

Fedir RYKHTIK
  • 587
  • 1
  • 9
  • 18
3

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
Jim B
  • 24,081
  • 4
  • 36
  • 60
1

You may use the following commands:

@echo off
sc Stop <Name of Agentry Service Here>
timeout 30
sc start <Name of Agentry Service Here>
PeterJ
  • 135
  • 4
  • 4
  • 15
Ganesh
  • 21
  • 1
0

I made a hybrid: in *.cmd file:

powershell -Command "& {Restart-Service MyService;}"