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?
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?
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:
May be missing something, but I use this all the time:
or shorter:
Dead simple with powershell:
Even better, using display names:
Get-Help Restart-Service
for moreIf it is purely for restarting the service, you can use
However, if you want access to the options of sc, you can use the start /wait command
this technique is a more general solution that can be applied to any command.
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.
It would be helpful for automatic script execution.
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 withnet start "myserviceshortname"
In vbscipt it's a little more code to stop a service and its' dependants:
Here's starting a service and anything it depends on (this should be familiar)
You may use the following commands:
I made a hybrid: in *.cmd file: