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:
However, you are out of luck if your entry has a generic ID or is 0, as pictured here:
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:
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:
Launch the Windows Event Viewer.
To do so, press the Windows + R keys to open the Run window, and then type eventvwr.msc.
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:
Right-click the event’s row and select Attach Task To This Event:
That will launch the Create Basic Task Wizard, which is part of the Task Scheduler.
In the wizard, give your task a friendly name. We’re called ours “Restart Service When Event 1026 Occurs”:
Click Next to continue.
Confirm the details of your trigger and click Next to move on:
Choose Start a program and click Next to continue:
Enter the full path to the batch file you created in step 1.
Click Next once done.
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.
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.
Click OK to continue.
As a security precaution, please enter your password:
And finally, the Event Viewer confirms that it has created your new task:
Click OK to wrap up.
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:
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:
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:
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!