The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


Q&A: How do I Automatically Restart my Windows Service when it Crashes?

How do I automatically restart my Windows service when it crashes?
  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’t there a way to restart the service automatically after it crashes? Please help.

— Kirkland

Hi Kirkland.

A Windows Service is Microsoft’s technology of choice for mission-critical applications that must run 24/7/365. As such, it’s incredibly frustrating when a service crashes and fails to deliver on that primary task!

Not to worry though, we’ve got your back. Here are a couple of ways to ensure that your Windows Service rebounds quickly after a crash (or other failure).

Solution #1: Configure Service Recovery (Basic)

Good news! Windows Services come with a built-in recovery mechanism.

If you open your troublesome service in the Services application, you will notice the Recovery tab:

Services Recovery Tab

There, you can specify what happens the first, second and subsequent times that your service fails.

Windows can perform one of the following three actions in response to a failure:

  • Restart the service

  • Restart the computer

  • Run a program

Or you can select “Take no action” to do nothing at all. That is the default for new services.

If you choose to run a program, you must specify the full path to the program to run, along with any parameters required.

For your situation, we recommend that you set all failure actions to “Restart the Service”:

Recovery Tab: Restart the Service

Simple, right? Well, maybe not. While the actions are straightforward, exactly what defines a failure? Under what conditions will Windows invoke the actions you have specified (i.e. to restart the service)?

Let’s back up a bit and address that mystery.

Microsoft’s definition of service failure

From the technical documentation, a service fails if it:

  1. Terminates without reporting a status of SERVICE_STOPPED to the Service Control Manager, or

  2. Exits with a non-zero exit code.

Note that (b) applies only if the Enable actions for stops with errors 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.

To sum up, here is our final recommended recovery configuration:

Recovery Tab: Recommended settings

Next, let’s look at where Windows Service recovery does a great job and where it’s not quite up to the task.

What service interruptions are covered by the Recovery tab?

The recovery settings above will cover a wide array of interruptions of your service.

For example, Windows will restart your service if:

  • The service ends unexpectedly

  • The service’s process is terminated (e.g. someone kills it from Task Manager)

  • The service exits with a non-zero error code

What service interruptions are NOT covered by the Recovery tab?

Sadly, the following disruptions are not handled by the basic recovery settings:

  • The service fails to start at boot

  • Someone accidentally stops the service from Services (or using the NET or SC commands)

  • The service stops unexpectedly but exits with code 0 (e.g. due to a bug in the software)

  • An internal component of the service crashes but the service shuts down normally

  • The service has a memory leak and stops working after it runs for a while

Other ways that Service Recovery falls short

Besides the above:

  • Service failures will be silent. That’s because the recovery options do not include notification. As a result, you may not find out when your service fails to start.

  • There is no protection in “zombie” situations — where your service is running but isn’t working as expected. Unfortunately, the recovery options are not sophisticated enough to look beyond the service’s status.

Solution #2: Deploy Service Protector (Advanced)

For comprehensive protection against a wide range of problems — including the ones listed above — you should use our Service Protector software.

Service Protector keeps any Windows Service running 24/7/365. Whenever a service stops — no matter the reason — Service Protector will immediately restart it.

Here’s a look at Service Protector babysitting the Windows print spooler service on our Server 2022 machine:

The Print Spooler service is protected

But Service Protector is much more than automatic restarts. Here are some of its key features:

Stuck service detection

Service Protector will handle non-responsive services that become stuck in the “Stopping” or “Starting” states for too long.

CPU hog detection

You can tell Service Protector to restart your service if it “runs hot” for a long period.

Memory leak/hog detection

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.

Email alerts when things go wrong

To ensure that you are kept in the loop, Service Protector can send you an email whenever your service fails.

Extensibility

Extend Service Protector with your own, customized failure detection plugins. Use them to detect and restart “zombie” Windows Services.

Workflow

Need to perform some housekeeping tasks before your service starts? Service Protector can run a batch file (or program) before restarting your Windows Service.

Scheduled restarts

Sometimes software that has been running for too long doesn’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 “fresh”.

Daily/Weekly reports

Service Protector will email you summaries of restarts, crashes, etc.

Error control

Service Protector will automatically dismiss those annoying “I have crashed!” message boxes that can halt the action.

Restores disabled services

If some clever person disables your service from the Services application, Service Protector will automatically re-enable it.

Try Service Protector for Free

Best of all, you can setup your service with Service Protector and see how it works completely free for 30 days. No need to make a purchase, register your email or provide a credit card. Just download and install.

Finally, while the Recovery tab is free, a perpetual license of Service Protector costs $99.99.

Nevertheless, if you are operating in a commercial environment where downtime is costly, we recommend making that modest investment to bulletproof your servers.

Best of luck with your service!

Posted in Windows Services | Tagged , , , , , | 2 Comments

2 Responses to Q&A: How do I Automatically Restart my Windows Service when it Crashes?

  1. Don Hartman says:

    Does Service Protector and/or Service Scheduler have to be re-initiated if the computer reboots?

  2. Hi Don.

    The answer is no.

    Service Protector creates a Windows Service that starts automatically whenever your computer boots. You don’t have to do anything.

    Service Scheduler creates entries in the Windows Task Scheduler and those are preserved and run on schedule after a reboot. Again, you don’t have to do anything yourself.

Leave a Reply

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