{"id":10828,"date":"2022-03-11T12:09:52","date_gmt":"2022-03-11T20:09:52","guid":{"rendered":"https:\/\/www.coretechnologies.com\/blog\/?p=10828"},"modified":"2023-09-03T22:47:12","modified_gmt":"2023-09-04T05:47:12","slug":"auto-restart-service","status":"publish","type":"post","link":"https:\/\/www.coretechnologies.com\/blog\/windows-services\/auto-restart-service\/","title":{"rendered":"Q&#038;A: How do I Automatically Restart my Windows Service when it Crashes?"},"content":{"rendered":"<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"\/blog\/images\/qa-restart-windows-service.webp\" style=\"margin-bottom:20px;\" title=\"How do I automatically restart my Windows service when it crashes?\" alt=\"How do I automatically restart my Windows service when it crashes?\" border=\"0\" width=\"380\" height=\"160\"><\/div>\n<div class=\"blog-qa-question-box\">\n<img loading=\"lazy\" decoding=\"async\" class=\"no-lazy-load\" src=\"https:\/\/cdn.coretechnologies.com\/images\/quotes-transparent-21x21.png\" width=\"21\" height=\"21\">&nbsp;&nbsp;One of our Windows Services crashes a few times a month. When that happens, a tech has to log into the server and start the service again, which is a pain. Isn&#8217;t there a way to restart the service automatically after it crashes? Please help.<\/p>\n<p align=\"right\">&mdash; Kirkland<\/p>\n<\/div>\n<p>Hi Kirkland.<\/p>\n<p>A <a href=\"\/WindowsServices\/FAQ.html#WhatIsAWindowsService\">Windows Service<\/a> is Microsoft&#8217;s technology of choice for mission-critical applications that must run 24\/7\/365. As such, it&#8217;s <b>incredibly frustrating<\/b> when a service crashes and fails to deliver on that primary task!<\/p>\n<p>Not to worry though, we&#8217;ve got your back. Here are a couple of ways to ensure that your Windows Service rebounds quickly after a crash (or other failure).<\/p>\n<h2 class=\"blog-caption\">Solution #1: Configure Service Recovery (Basic)<\/h2>\n<p>Good news! Windows Services come with a built-in recovery mechanism.<\/p>\n<p>If you open your troublesome service in the <a href=\"\/blog\/windows-services\/essential-tools-windows-services-msc\/\">Services application<\/a>, you will notice the Recovery tab:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/spooler-service-recovery-tab.png\" class=\"zoomPopup\" title=\"Services Recovery Tab\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/spooler-service-recovery-tab.png\" title=\"Services Recovery Tab (click to enlarge)\" alt=\"Services Recovery Tab\" border=\"0\" width=\"406\" height=\"468\" \/><\/a><\/div>\n<p>There, you can specify what happens the first, second and subsequent times that your service fails.<\/p>\n<p>Windows can perform one of the following three actions in response to a failure:<\/p>\n<ul>\n<li>\n<p>Restart the service<\/p>\n<\/li>\n<li>\n<p>Restart the computer<\/p>\n<\/li>\n<li>\n<p>Run a program<\/p>\n<\/li>\n<\/ul>\n<p>Or you can select &#8220;Take no action&#8221; to do nothing at all. That is the default for new services.<\/p>\n<p>If you choose to run a program, you must specify the full path to the program to run, along with any parameters required.<\/p>\n<p>For your situation, we recommend that you set all failure actions to &#8220;Restart the Service&#8221;:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/recovery-tab-restart-service.png\" class=\"zoomPopup\" title=\"Recovery Tab: Restart the Service\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/recovery-tab-restart-service.png\" title=\"Recovery Tab: Restart the Service (click to enlarge)\" alt=\"Recovery Tab: Restart the Service\" border=\"0\" width=\"406\" height=\"468\" \/><\/a><\/div>\n<p>Simple, right? Well, maybe not. While the actions are straightforward, <b>exactly what defines a failure<\/b>? Under what conditions will Windows invoke the actions you have specified (i.e. to restart the service)?<\/p>\n<p>Let&#8217;s back up a bit and address that mystery.<\/p>\n<h3 class=\"blog-caption\">Microsoft&#8217;s definition of service failure<\/h3>\n<p>From the <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/api\/winsvc\/ns-winsvc-service_failure_actions_flag\" target=\"_blank\" rel=\"noopener\">technical documentation<\/a>, a service fails if it:<\/p>\n<ol type=\"a\">\n<li>\n<p>Terminates without reporting a status of SERVICE_STOPPED to the <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/services\/service-control-manager\" target=\"_blank\" rel=\"noopener\">Service Control Manager<\/a>, or<\/p>\n<\/li>\n<li>\n<p>Exits with a non-zero exit code.<\/p>\n<\/li>\n<\/ol>\n<p>Note that (b) applies only if the <b>Enable actions for stops with errors<\/b> box on the Recovery tab is checked. You should definitely enable that option because doing so will allow Windows to catch a wider range of failures of your misbehaving service.<\/p>\n<p>To sum up, here is our final recommended recovery configuration:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/recovery-tab-recommended-settings.png\" class=\"zoomPopup\" title=\"Recovery Tab: Recommended settings\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/recovery-tab-recommended-settings.png\" title=\"Recovery Tab: Recommended settings (click to enlarge)\" alt=\"Recovery Tab: Recommended settings\" border=\"0\" width=\"406\" height=\"468\" \/><\/a><\/div>\n<p>Next, let&#8217;s look at where Windows Service recovery does a great job and where it&#8217;s not quite up to the task.<\/p>\n<h3 class=\"blog-caption\">What service interruptions are covered by the Recovery tab?<\/h3>\n<p>The recovery settings above will cover a wide array of interruptions of your service.<\/p>\n<p>For example, Windows will restart your service if:<\/p>\n<ul>\n<li>\n<p>The service <b>ends unexpectedly<\/b><\/p>\n<\/li>\n<li>\n<p>The service&#8217;s <b>process is terminated<\/b> (e.g. someone kills it from Task Manager)<\/p>\n<\/li>\n<li>\n<p>The service exits with a <b>non-zero error code<\/b><\/p>\n<\/li>\n<\/ul>\n<h3 class=\"blog-caption\">What service interruptions are NOT covered by the Recovery tab?<\/h3>\n<p>Sadly, the following disruptions are not handled by the basic recovery settings:<\/p>\n<ul>\n<li>\n<p>The service <b>fails to start at boot<\/b><\/p>\n<\/li>\n<li>\n<p>Someone accidentally <b>stops the service<\/b> from Services (or using the NET or SC commands)<\/p>\n<\/li>\n<li>\n<p>The service stops unexpectedly but <b>exits with code 0<\/b> (e.g. due to a bug in the software)<\/p>\n<\/li>\n<li>\n<p>An internal component of the service crashes but the service <b>shuts down normally<\/b><\/p>\n<\/li>\n<li>\n<p>The service has a memory leak and <b>stops working<\/b> after it runs for a while<\/p>\n<\/li>\n<\/ul>\n<h3 class=\"blog-caption\">Other ways that Service Recovery falls short<\/h3>\n<p>Besides the above:<\/p>\n<ul>\n<li>\n<p><b>Service failures will be silent<\/b>. That&#8217;s because the recovery options do not include notification. As a result, you may not find out when your service fails to start.<\/p>\n<\/li>\n<li>\n<p>There is <b>no protection in &#8220;zombie&#8221; situations<\/b> &mdash; where your service is running but isn&#8217;t working as expected. Unfortunately, the recovery options are not sophisticated enough to look beyond the service&#8217;s status.<\/p>\n<\/li>\n<\/ul>\n<h2 class=\"blog-caption\">Solution #2: Deploy Service Protector (Advanced)<\/h2>\n<p>For comprehensive protection against a wide range of problems &mdash; including the  ones listed above &mdash; you should use our <a href=\"\/products\/ServiceProtector\/\">Service Protector<\/a> software.<\/p>\n<p>Service Protector keeps any Windows Service running 24\/7\/365. Whenever a service stops &mdash; no matter the reason &mdash; Service Protector will immediately restart it.<\/p>\n<p>Here&#8217;s a look at Service Protector babysitting the Windows <a href=\"\/blog\/windows-services\/spooler\/\">print spooler service<\/a> on our Server 2022 machine:<\/p>\n<div align=\"center\"><a href=\"\/blog\/images\/spooler-service-protected.png\" class=\"zoomPopup\" title=\"The Print Spooler service is protected\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"image-padding\" src=\"\/blog\/images\/spooler-service-protected.png\" title=\"The Print Spooler service is protected (click to enlarge)\" alt=\"The Print Spooler service is protected\" border=\"0\" width=\"520\" \/><\/a><\/div>\n<p>But Service Protector is much more than automatic restarts. Here are some of its key features:<\/p>\n<h3 class=\"blog-caption\">Stuck service detection<\/h3>\n<p>Service Protector will handle non-responsive services that become stuck in the &#8220;Stopping&#8221; or &#8220;Starting&#8221; states for too long.<\/p>\n<h3 class=\"blog-caption\">CPU hog detection<\/h3>\n<p>You can tell Service Protector to restart your service if it &#8220;runs hot&#8221; for a long period.<\/p>\n<h3 class=\"blog-caption\">Memory leak\/hog detection<\/h3>\n<p>A Windows Service that constantly eats up RAM can lead to disaster. Service Protector will terminate and recycle leaky services, to free up accumulated memory and restore normal operation.<\/p>\n<h3 class=\"blog-caption\">Email alerts when things go wrong<\/h3>\n<p>To ensure that you are kept in the loop, Service Protector can send you an email whenever your service fails.<\/p>\n<h3 class=\"blog-caption\">Extensibility<\/h3>\n<p>Extend Service Protector with your own, <a href=\"\/products\/AlwaysUp\/Plugins\/\">customized failure detection plugins<\/a>. Use them to detect and restart &#8220;zombie&#8221; Windows Services.<\/p>\n<h3 class=\"blog-caption\">Workflow<\/h3>\n<p>Need to perform some housekeeping tasks before your service starts? Service Protector can run a batch file (or program) before restarting your Windows Service.<\/p>\n<h3 class=\"blog-caption\">Scheduled restarts<\/h3>\n<p>Sometimes software that has been running for too long doesn&#8217;t work. Service Protector can restart your service (or reboot the PC) at specific times, to minimize the effects of memory leaks and keep your service &#8220;fresh&#8221;.<\/p>\n<h3 class=\"blog-caption\">Daily\/Weekly reports<\/h3>\n<p>Service Protector will email you summaries of restarts, crashes, etc.<\/p>\n<h3 class=\"blog-caption\">Error control<\/h3>\n<p>Service Protector will automatically dismiss those annoying &#8220;I have crashed!&#8221; message boxes that can halt the action.<\/p>\n<h3 class=\"blog-caption\">Restores disabled services<\/h3>\n<p>If some clever person disables your service from the Services application, Service Protector will automatically re-enable it.<\/p>\n<h3 class=\"blog-caption\">Try Service Protector for Free<\/h3>\n<p>Best of all, you can setup your service with Service Protector and see how it works <b>completely free for 30 days<\/b>. No need to make a purchase, register your email or provide a credit card. Just <a href=\"\/products\/ServiceProtector\/HowToInstall.html\">download and install<\/a>.<\/p>\n<p style=\"margin-top:30px\">\nFinally, while the Recovery tab is free, a perpetual license of Service Protector costs $99.99.\n<\/p>\n<p>Nevertheless, if you are operating in a commercial environment where downtime is costly, we recommend making that modest investment to bulletproof your servers.<\/p>\n<p>Best of luck with your service!<\/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\/windows-services\/non-admins-control-services-remotely\/\"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 Can&#039;t Non-Admins Start\/Stop Windows Services Remotely?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/remote-access-check-exemption-list-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 Can&#039;t Non-Admins Start\/Stop Windows Services Remotely?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/regular-users-restart-windows-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 Let Regular Users Monitor and Restart a Windows Service?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/uesr-security-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 Let Regular Users Monitor and Restart a Windows Service?<\/h2><\/div><\/div><\/a><a href=\"https:\/\/www.coretechnologies.com\/blog\/alwaysup\/google-drive-updates\/\"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 get AlwaysUp to Run the Latest Google Drive for desktop?\"  src=\"https:\/\/www.coretechnologies.com\/blog\/wp-content\/uploads\/google-drive-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 get AlwaysUp to Run the Latest Google Drive for desktop?<\/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;One of our Windows Services crashes a few times a month. When that happens, a tech has to log into the server and start the service again, which is a pain. Isn&#8217;t there a way to restart the service automatically &hellip; <a href=\"https:\/\/www.coretechnologies.com\/blog\/windows-services\/auto-restart-service\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":10980,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[276,127,147,279,277,196],"class_list":["post-10828","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-windows-services","tag-failure-actions","tag-qa","tag-service-protector-tag","tag-service-recovery","tag-watchdog","tag-windows-services-2"],"_links":{"self":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10828","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=10828"}],"version-history":[{"count":27,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10828\/revisions"}],"predecessor-version":[{"id":12128,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/posts\/10828\/revisions\/12128"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media\/10980"}],"wp:attachment":[{"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/media?parent=10828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/categories?post=10828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.coretechnologies.com\/blog\/wp-json\/wp\/v2\/tags?post=10828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}