{"id":10282,"date":"2021-04-26T06:14:21","date_gmt":"2021-04-26T13:14:21","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=10282"},"modified":"2022-09-04T06:35:37","modified_gmt":"2022-09-04T13:35:37","slug":"restart-service-when-event-reported","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/windows-services\/restart-service-when-event-reported\/","title":{"rendered":"Q&#038;A: How do I Restart a Windows Service when an Event is Reported?"},"content":{"rendered":"<div align=\"center\"><img decoding=\"async\" src=\"\/blog\/images\/qa-restart-service-on-event.png\" style=\"margin-bottom:20px;\" title=\"Q&#038;A: How do I Restart a Windows Service when an Event is Logged?\" alt=\"Q&#038;A: How do I Restart a Windows Service when an Event is Logged?\" border=\"0\"><\/div>\n<div class=\"blog-qa-question-box\">\n<img decoding=\"async\" src=\"\/images\/quotes-transparent-21x21.png\">&nbsp;&nbsp;We have a service that should restart whenever another service writes a particular event to the Event Viewer. How can I do that?<\/p>\n<p align=\"right\">&mdash; Russell<\/p>\n<\/div>\n<p>Hi Russell.<\/p>\n<p>To create that workflow, you can introduce an &#8220;Event Trigger&#8221; with the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Windows_Task_Scheduler\" target=\"_blank\" rel=\"noopener\">Windows Task Scheduler<\/a>. However, that will only work if your triggering event is well defined, with a unique, non-zero identifier.<\/p>\n<p>For example, <a href=\"https:\/\/stackoverflow.com\/questions\/23189482\/net-runtime-error-event-id-1026\" target=\"_blank\" rel=\"noopener\">event 1026<\/a> &mdash; reported when a .NET application crashes &mdash; is an excellent candidate:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/event-1026-net-runtime-error.png\" class=\"zoomPopup\" title=\"Event 1026: .NET Runtime Error\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/event-1026-net-runtime-error.png\" title=\"Event 1026: .NET Runtime Error (click to enlarge)\" alt=\"Event 1026: .NET Runtime Error\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>However, you are out of luck if your entry has a generic ID or is 0, as pictured here:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/events-with-id-0.png\" class=\"zoomPopup\" title=\"Events with ID 0\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/events-with-id-0.png\" title=\"Events with ID 0 (click to enlarge)\" alt=\"Events with ID 0\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Unfortunately there is no good way to create a trigger based on a poorly defined event.<\/p>\n<h2 class=\"blog-caption\">How to setup an &#8220;Event Trigger&#8221; Task that restarts your Windows Service<\/h2>\n<p>Assuming that your event has a unique ID, here&#8217;s the step-by-step process to recycle your service when the event arrives:<\/p>\n<ol>\n<li>\n<p>First, create a batch file that restarts your service.<\/p>\n<p>The file should contain two commands &mdash; one to stop your service and another to start it again, like this:<\/p>\n<div class=\"code-box\">\nNET STOP &lt;Service-Name&gt;<br \/>\nNET START &lt;Service-Name&gt;\n<\/div>\n<p>&lt;Service-Name&gt; is the <a href=\"\/WindowsServices\/FAQ.html#FindServiceName\">name of your Windows Service<\/a>.<\/p>\n<p>For example, here is the batch file we created to recycle the Print Spooler service:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-service-batch-file.png\" class=\"zoomPopup\" title=\"Restart service batch file\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-service-batch-file.png\" title=\"Restart service batch file (click to enlarge)\" alt=\"Restart service batch file\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>Launch the Windows Event Viewer.<\/p>\n<p>To do so, press the <b>Windows + R<\/b> keys to open the <b>Run<\/b> window, and then type <b>eventvwr.msc<\/b>.<\/p>\n<\/li>\n<li>\n<p>In the Event Viewer, navigate to find and highlight an instance of the entry that should trigger your service to restart.<\/p>\n<p>To illustrate, we&#8217;ll work with event 1026 in the Application log:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/event-viewer-application-event-1026.png\" class=\"zoomPopup\" title=\"Event Viewer: Application event 1026\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/event-viewer-application-event-1026.png\" title=\"Event Viewer: Application event 1026 (click to enlarge)\" alt=\"Event Viewer: Application event 1026\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>Right-click the event&#8217;s row and select <b>Attach Task To This Event<\/b>:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/attach-task-to-event-1026.png\" class=\"zoomPopup\" title=\"Attach a task to the event\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/attach-task-to-event-1026.png\" title=\"Attach a task to the event (click to enlarge)\" alt=\"Attach a task to the event\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>That will launch the <b>Create Basic Task Wizard<\/b>, which is part of the Task Scheduler.<\/p>\n<\/li>\n<li>\n<p>In the wizard, give your task a friendly name. We&#8217;re called ours &#8220;Restart Service When Event 1026 Occurs&#8221;:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/create-event-trigger-task-name.png\" class=\"zoomPopup\" title=\"Create event trigger task: Name\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/create-event-trigger-task-name.png\" title=\"Create event trigger task: Name (click to enlarge)\" alt=\"Create event trigger task: Name\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Click <b>Next<\/b> to continue.<\/p>\n<\/li>\n<li>\n<p>Confirm the details of your trigger and click <b>Next<\/b> to move on:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/create-event-trigger-task-details.png\" class=\"zoomPopup\" title=\"Create event trigger task: Event details\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/create-event-trigger-task-details.png\" title=\"Create event trigger task: Event details (click to enlarge)\" alt=\"Create event trigger task: Event details\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>Choose <b>Start a program<\/b> and click <b>Next<\/b> to continue:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/create-event-trigger-task-start-program.png\" class=\"zoomPopup\" title=\"Create event trigger task: Start a program\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/create-event-trigger-task-start-program.png\" title=\"Create event trigger task: Start a program (click to enlarge)\" alt=\"Create event trigger task: Start a program\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>Enter the full path to the batch file you created in step 1.<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/create-event-trigger-task-batch-file.png\" class=\"zoomPopup\" title=\"Create event trigger task: Enter batch file\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/create-event-trigger-task-batch-file.png\" title=\"Create event trigger task: Enter batch file (click to enlarge)\" alt=\"Create event trigger task: Enter batch file\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Click <b>Next<\/b> once done.<\/p>\n<\/li>\n<li>\n<p>Review the summary and make sure everything looks good. This is your last chance to go back and make changes before creating the task.<\/p>\n<p>Check the <b>Open the Properties dialog for this task when I click Finish<\/b> box. We&#8217;ll need to tweak a few settings to ensure that your service restarts smoothly from the batch file.<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/create-event-trigger-task-summary.png\" class=\"zoomPopup\" title=\"Create event trigger task: Summary\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/create-event-trigger-task-summary.png\" title=\"Create event trigger task: Summary (click to enlarge)\" alt=\"Create event trigger task: Summary\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>In the Properties window, affirm a couple of options:<\/p>\n<p><b>Run whether user is logged on or not<\/b>. Otherwise, your service will only restart when you&#8217;re logged on.<\/p>\n<p><b>Run with highest privileges<\/b>. Necessary because restarting your service likely requires administrative rights.<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/create-event-trigger-task-properties.png\" class=\"zoomPopup\" title=\"Create event trigger task: Properties\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/create-event-trigger-task-properties.png\" title=\"Create event trigger task: Properties (click to enlarge)\" alt=\"Create event trigger task: Properties\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>Click <b>OK<\/b> to continue.<\/p>\n<\/li>\n<li>\n<p>As a security precaution, please enter your password:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/task-scheduler-enter-password.png\" class=\"zoomPopup\" title=\"Create event trigger task: Password\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/task-scheduler-enter-password.png\" title=\"Create event trigger task: Password (click to enlarge)\" alt=\"Create event trigger task: Password\" border=\"0\" \/><\/a><\/div>\n<\/li>\n<li>\n<p>And finally, the Event Viewer confirms that it has created your new task:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/new-task-confirmed.png\" class=\"zoomPopup\" title=\"Create event trigger task: Confirmed\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/new-task-confirmed.png\" title=\"Create event trigger task: Confirmed (click to enlarge)\" alt=\"Create event trigger task: Confirmed\" border=\"0\" \/><\/a><\/div>\n<p>Click <b>OK<\/b> to wrap up.<\/p>\n<\/li>\n<\/ol>\n<p style=\"margin-top:30px\">That&#8217;s it!<\/p>\n<p>The next time the event enters the Windows Event Logs, the Task Scheduler will run the batch file, which will restart your service.<\/p>\n<h2 class=\"blog-caption\">Managing the new &#8220;Event Trigger&#8221; Task<\/h2>\n<p>The new task will reside in the <b>Event Viewer Tasks<\/b> section of the Task Scheduler:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/new-event-viewer-task.png\" class=\"zoomPopup\" title=\"Task Scheduler: New Event Viewer task\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/new-event-viewer-task.png\" title=\"Task Scheduler: New Event Viewer task (click to enlarge)\" alt=\"Task Scheduler: New Event Viewer task\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>You can make adjustments to the task there. <\/p>\n<p>One final Task Scheduler tip: If you see a link labeled <b>Enable All Tasks History<\/b> in the <b>Actions<\/b> section on the right, be sure to click it:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/enable-all-tasks-history.png\" class=\"zoomPopup\" title=\"Task Scheduler: Enable All Tasks History\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/enable-all-tasks-history.png\" title=\"Task Scheduler: Enable All Tasks History (click to enlarge)\" alt=\"Task Scheduler: Enable All Tasks History\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>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&#8217;s <b>History<\/b> tab to confirm that the task ran and your batch file executed successfully.<\/p>\n<h2 class=\"blog-caption\">Bonus #1: Send an email alert when the service restarts<\/h2>\n<p>Would you like to be notified whenever the event appears and the service restarts?<\/p>\n<p>If so, you should update your script to call a command line tool that will deliver an email message.<\/p>\n<p>We recommend using a PowerShell script, or SwithMail, as described in <a href=\"\/blog\/windows-services\/send-email-without-outlook\/\">this article on sending email without Outlook<\/a>.<\/p>\n<h2 class=\"blog-caption\">Bonus #2: Use ServicePilot to restart a &#8220;slow&#8221; service that takes a while to stop<\/h2>\n<p>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&#8217;t in the right state. The end result is a dead service!<\/p>\n<p>In that case, we recommend using our free <a href=\"\/products\/ServicePilot\/\">ServicePilot<\/a> utility instead of the NET command.<\/p>\n<p>For example, here is our script that replaces NET with ServicePilot:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/restart-service-with-servicepilot.png\" class=\"zoomPopup\" title=\"Restart the service with ServicePilot\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/restart-service-with-servicepilot.png\" title=\"Restart the service with ServicePilot (click to enlarge)\" alt=\"Restart the service with ServicePilot\" border=\"0\" \/><\/a><\/div>\n<p>Notice the option to give the service up to 120 seconds to stop. You should specify a duration that works for your service.<\/p>\n<p style=\"margin-top:30px;\">\nAll the best!<\/p>\n<!-- relpost-thumb-wrapper --><div class=\"relpost-thumb-wrapper\"><!-- filter-class --><div class=\"relpost-thumb-container\"><style>.relpost-block-single-image, .relpost-post-image { margin-bottom: 10px; }<\/style><h3>You may also like...<\/h3><div style=\"clear: both\"><\/div><div style=\"clear: both\"><\/div><!-- relpost-block-container --><div class=\"relpost-block-container relpost-block-column-layout\" style=\"--relposth-columns: 3;--relposth-columns_t: 2; --relposth-columns_m: 2\"><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/start-java-socket-application\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: Why won&#039;t AlwaysUp Start my Java Application?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/java-application-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: Why won&#039;t AlwaysUp Start my Java Application?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/chatgpt\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"ChatGPT Knows Windows Services\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/chatgpt-ai-windows-services-150x150-1.webp\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">ChatGPT Knows Windows Services<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/auto-restart-service\/\"class=\"relpost-block-single\" ><div class=\"relpost-custom-block-single\"><img decoding=\"async\" loading=\"lazy\" class=\"relpost-block-single-image\" alt=\"Q&amp;A: How do I Automatically Restart my Windows Service when it Crashes?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/restart-windows-service-150x150-1.png\" style=\"aspect-ratio:1\/1\" style=\"aspect-ratio:1\/1\"><\/img><div class=\"relpost-block-single-text\"  style=\"height: 75px;font-family: Arial;  font-size: 12px;  color: #333333;\"><h2 class=\"relpost_card_title\">Q&amp;A: How do I Automatically Restart my Windows Service when it Crashes?<\/h2><\/div><\/div><\/a><\/div><!-- close relpost-block-container --><div style=\"clear: both\"><\/div><\/div><!-- close filter class --><\/div><!-- close relpost-thumb-wrapper -->","protected":false},"excerpt":{"rendered":"<p>&nbsp;&nbsp;We have a service that should restart whenever another service writes a particular event to the Event Viewer. How can I do that? &mdash; Russell Hi Russell. To create that workflow, you can introduce an &#8220;Event Trigger&#8221; with the Windows &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/restart-service-when-event-reported\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10396,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[79,127,145,164,196],"class_list":["post-10282","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows-services","tag-event-viewer","tag-qa","tag-service-pilot","tag-task-scheduler","tag-windows-services-2"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/comments?post=10282"}],"version-history":[{"count":21,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10282\/revisions"}],"predecessor-version":[{"id":11496,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10282\/revisions\/11496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/10396"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=10282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=10282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=10282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}