Recipe 7.6. Performing an Action Automatically When a Service FailsProblemYou want to perform an action automatically when a service fails. SolutionUsing a graphical user interface
Using a command-line interface:The following command causes the MyMonitor service to automatically restart after two failures and reboot on the third. Each failure can be up to five seconds apart. > sc failure MyMonitor reset= 3600 reboot="Restarting due to repeated MyMonitor failure" actions= restart/5000/restart/5000/reboot/5000 Using VBScriptThe Win32_Service class does not support setting the recovery options of a service. DiscussionWindows 2000 added a new capability that was sorely missing in previous versions of Windows: the ability to automatically perform a specified action when a service fails. You can configure an action to occur after the first, second, and subsequent failures. The actions you can take include no action (the default), restarting the service, running a program, or restarting the computer. Restarting the service or computer should be used as a last resort because they could mask underlying problems. You can also reset the failure count after a certain number of days. So let's say that you have a service that regularly fails multiple times a day, but you only want it to restart itself after the first failure every day. You would configure the first failure action to restart the service and set the reset fail count to 1 day. You can even combine multiple actions by choosing to run a program after service failure. You can create a simple batch file that restarts the service and emails you a report. This gives you a lot of flexibility in how you handle service failures. |