The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


Q&A: How do I Restart a Windows Service when an Event is Reported?

Q&A: How do I Restart a Windows Service when an Event is Logged?
  We have a service that should restart whenever another service writes a particular event to the Event Viewer. How can I do that?

— Russell

Hi Russell.

To create that workflow, you can introduce an “Event Trigger” with the Windows Task Scheduler. However, that will only work if your triggering event is well defined, with a unique, non-zero identifier.

For example, event 1026 — reported when a .NET application crashes — is an excellent candidate:

Event 1026: .NET Runtime Error

However, you are out of luck if your entry has a generic ID or is 0, as pictured here:

Events with ID 0

Unfortunately there is no good way to create a trigger based on a poorly defined event.

How to setup an “Event Trigger” Task that restarts your Windows Service

Assuming that your event has a unique ID, here’s the step-by-step process to recycle your service when the event arrives:

  1. First, create a batch file that restarts your service.

    The file should contain two commands — one to stop your service and another to start it again, like this:

    NET STOP <Service-Name>
    NET START <Service-Name>

    <Service-Name> is the name of your Windows Service.

    For example, here is the batch file we created to recycle the Print Spooler service:

    Restart service batch file
  2. Launch the Windows Event Viewer.

    To do so, press the Windows + R keys to open the Run window, and then type eventvwr.msc.

  3. In the Event Viewer, navigate to find and highlight an instance of the entry that should trigger your service to restart.

    To illustrate, we’ll work with event 1026 in the Application log:

    Event Viewer: Application event 1026
  4. Right-click the event’s row and select Attach Task To This Event:

    Attach a task to the event

    That will launch the Create Basic Task Wizard, which is part of the Task Scheduler.

  5. In the wizard, give your task a friendly name. We’re called ours “Restart Service When Event 1026 Occurs”:

    Create event trigger task: Name

    Click Next to continue.

  6. Confirm the details of your trigger and click Next to move on:

    Create event trigger task: Event details
  7. Choose Start a program and click Next to continue:

    Create event trigger task: Start a program
  8. Enter the full path to the batch file you created in step 1.

    Create event trigger task: Enter batch file

    Click Next once done.

  9. Review the summary and make sure everything looks good. This is your last chance to go back and make changes before creating the task.

    Check the Open the Properties dialog for this task when I click Finish box. We’ll need to tweak a few settings to ensure that your service restarts smoothly from the batch file.

    Create event trigger task: Summary
  10. In the Properties window, affirm a couple of options:

    Run whether user is logged on or not. Otherwise, your service will only restart when you’re logged on.

    Run with highest privileges. Necessary because restarting your service likely requires administrative rights.

    Create event trigger task: Properties

    Click OK to continue.

  11. As a security precaution, please enter your password:

    Create event trigger task: Password
  12. And finally, the Event Viewer confirms that it has created your new task:

    Create event trigger task: Confirmed

    Click OK to wrap up.

That’s it!

The next time the event enters the Windows Event Logs, the Task Scheduler will run the batch file, which will restart your service.

Managing the new “Event Trigger” Task

The new task will reside in the Event Viewer Tasks section of the Task Scheduler:

Task Scheduler: New Event Viewer task

You can make adjustments to the task there.

One final Task Scheduler tip: If you see a link labeled Enable All Tasks History in the Actions section on the right, be sure to click it:

Task Scheduler: Enable All Tasks History

That setting tells Task Scheduler to keep a record every time a task runs. And with that tracking in place, after your event has been reported you can check the task’s History tab to confirm that the task ran and your batch file executed successfully.

Bonus #1: Send an email alert when the service restarts

Would you like to be notified whenever the event appears and the service restarts?

If so, you should update your script to call a command line tool that will deliver an email message.

We recommend using a PowerShell script, or SwithMail, as described in this article on sending email without Outlook.

Bonus #2: Use ServicePilot to restart a “slow” service that takes a while to stop

The NET command can run into trouble if your service takes more than 30 seconds to stop. In that scenario, NET STOP gives up and the subsequent NET START will fail because the service isn’t in the right state. The end result is a dead service!

In that case, we recommend using our free ServicePilot utility instead of the NET command.

For example, here is our script that replaces NET with ServicePilot:

Restart the service with ServicePilot

Notice the option to give the service up to 120 seconds to stop. You should specify a duration that works for your service.

All the best!

Posted in Windows Services | Tagged , , , , | Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *