Recipe 10.20. Performing an Action Automatically When a Service Fails


Problem

You want to perform an action automatically when a service fails.

Solution

Using a graphical user interface

  1. Open the Services snap-in.

  2. In the left pane, double-click on the service you want to configure.

  3. Click the Recovery tab.

  4. Configure the failure options you want and click OK.

Using a command-line interface

The following command causes the MyMonitor service to be automatically restarted after two failures and then reboots on the third failure. Each failure can be up to 5 seconds apart:

> sc failure MyMonitor reset= 3600 reboot="Restarting do to repeated  MyMonitor failure" actions= restart/5000/restart/5000/reboot/5000

Using VBScript

The Win32_Service class does not support setting the recovery options for a service.

Discussion

Windows 2000 added a new capability that was sorely missing in previous versions: the ability to automatically perform an action when a service fails. You can configure an action to occur after the first failure, second failure, or subsequent failures. The actions you can take include no action (the default), restarting the service, running a program, or restarting the computer.

You can also reset the failure count after a certain number of days. So let's say that you want to have a service restart itself after a failure, but no more than once a day. You would configure the first failure action to restart the service and set the reset fail count to one day.

You can also 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.



Windows XP Cookbook
Windows XP Cookbook (Cookbooks)
ISBN: 0596007256
EAN: 2147483647
Year: 2006
Pages: 408

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net