Windows – Restart a Windows service from a script

scriptingwindowswindows-command-promptwindows-server-2003windows-server-2008

I know there are several similar posts on the subject, but what I would like to know is how to re-start a service that has already been stopped. Is there a .bat or .vbs script that would check if the service is stop and then restart it if it's stopped and run in a loop?

Thank you for your help!

Update 7.IX.2012 @ 11.25 I understand there are several posts that go into details on usage of SC and net stop & net start, most of those posts are dealing with stopping the service first and then restarting it. I am dealing with the service that already either crashed or stopped and I need to check if it is stopped and then restart it. I hope I am making it clearer. It would also be nice to have a log file attached to it too.

Best Answer

As an example, create a batch file C:\derp.bat

Contents of batch file could look something like this:

net start "Service Name"

C:\derp.bat

First line: If the service is already running then nothing will happen. If it's not running then it will attempt to start.

Second line: restarts the batch file.

You could throw a ping 127.0.0.1 -n 60 in the middle if you wanted it to try every 60 seconds.