How can I automatically restart a Windows service if it crashes?

Solution 1:

Under the Services application, select the properties of the service in question.

View the recovery tab - there are all sorts of options - I'd set First & Second Failure to Restart the Service, Third to run a batch program that BLAT's out an email with the third failure notification.

You should also set the Reset Fail Count to 1 to reset the fail count daily.

EDIT:

Looks like you can do this via a command line:

SC failure w3svc reset= 432000  actions= restart/30000/restart/60000/run/60000
SC failure w3svc command= "MyBatchFile.cmd"

Your MyBatchFile.CMD file can look like this:

blat - -body "Service W3svc Failed" -subject "SERVICE ERROR" -to [email protected] -server SMTP.Example.com -f [email protected]

Solution 2:

Open Services.msc, double-click on the service to open the Properties of the service, there is a Recovery tab and those settings should allow you to restart the service upon failure.