Windows service restart on fail

Windows Services Recovery not restarting service

I configure the recovery for Windows services to restart with a one minute delay after failures. But I have never gotten it to actually restart the service (even with the most blatant errors).

I do get a message in the EventViewer:

The description for Event ID ( 1 ) in Source ( MyApp.exe ) cannot be found. The local computer may not have the necessary registry information or message DLL files to display messages from a remote computer. You may be able to use the /AUXSOURCE= flag to retrieve this description; see Help and Support for details. The following information is part of the event: Access violation at address 00429874 in module ‘MyApp.exe’. Write of address 00456704.

Is there something else I have to do? Is there something in my code (I use Delphi) which needs to be set to enable this?

4 Answers 4

Service Recovery is intended to handle the case where a service crashes — so if you go to taskmgr and right click «end process» on your service process, the recovery logic should kick in. I don’t believe that the service recovery logic kicks in if your service exits gracefully (even if it exits with an error).

Also the eventvwr message indicates that your application called the ReportEvent API specifying event ID 1. But you haven’t registered your event messages with the event viewer so it can’t convert event ID 1 into a meaningful text string.

Service Recovery only works for unexpected exit like (exit(-1)) call. For all the way we use to stop the service in usual way will not works for recovery. If you want to stop service and still wants recovery to work, call exit(-1) and you will see error message as «service stopped with unexpected error» , and then your service will restart as recovery setting is.

The Service Control Manager will attempt to restart your service if you’ve set it up to be restarted by the SCM. This is detailed here in the documentation for the SERVICE_FAILURE_ACTIONS structure.

A service is considered failed when it terminates without reporting a status of SERVICE_STOPPED to the service controller.

This can be fine tuned by setting the SERVICE_FAILURE_ACTIONS_FLAG structure’s fFailureActionsOnNonCrashFailures flag, see here). You can set this setting from the Services applet by checking the «Enable actions for stops with errors» checkbox on the recovery tab.

If this member is TRUE and the service has configured failure actions, the failure actions are queued if the service process terminates without reporting a status of SERVICE_STOPPED or if it enters the SERVICE_STOPPED state but the dwWin32ExitCode member of the SERVICE_STATUS structure is not ERROR_SUCCESS (0). If this member is FALSE and the service has configured failure actions, the failure actions are queued only if the service terminates without reporting a status of SERVICE_STOPPED.

So, depending on how you have structured your service, how you have configured your failure actions AND what you do when you have your ‘fatal error’ it may be enough to call ExitProcess() or exit() and return a non zero value. However, it’s probably safest to ensure that your service exits without the code that’s dealing with the SCM telling the SCM that your service has reached the SERVICE_STOPPED state. This ensures that your failure actions ALWAYS happen.

Windows service restart on fail

You could have a look at the blog post below, see if it helps you!

Читайте также:  Активация windows по почте

» First failure: what should occur the first time the service fails. Valid options are «Take No Action», «Restart the Service», «Run a Program», and «Restart the Computer».

Second failure: same options the second time a service fails

Subsequent failures: same options for any subsequent failure

Reset fail count after: the number of days the service must be running before the failure count is reset

Restart service after: the amount of time in minutes to wait to restart the service

This is very nice, but it is very easy to misunderstand what these values actually do. I have seen a number of services (and I tried this myself) set these values to 0 days and 0 minutes. The problem is your service will continually restart if you set the failure count to reset after 0 days, if the service at least started correctly. The result is only the first option («first failure») will ever be run.

To fix this, set the failure count to reset after one day. The drawback to this approach is your service may stay stopped after failing several times but this likely means something is toast anyways.

One thing also to take into account is not all services will work with the reset logic — or in other words just setting the recovery options on any service does not guarantee that it will restart. In order for the service to restart, it must exit abnormally. This generally means the service must exist with a non-zero exit code and the service status must not be stopped (note: this has changed for Vista — it is possible to set the service status to stopped and provide an exit code to trigger the restart logic).

Windows service restart on fail

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Asked by:

Question

I have developed a service and set it’s recovery options as;

  • First Failure: Restart the Service
  • Second Failure: Restart the Service
  • Subsequent failures: Restart the Service
  • Reset fail count after: 1 days
  • Restart service after: 0 minutes
  • Enable actions for stops with errors: checked

And Opened Task Manager.

If I kill service process using Details Tab and right click to process and select End Task then My Service restarts immediately.

But if i go to Processes Tab and background processes section and find my process then right click it and select End Task, then my service doesnt restart.

Is this a bug or i am missing something.

All replies

Check your system event log for Service Control Manager entries. I followed the steps you described and it is working as expected. Maybe your service can intercept the end task event and do a controlled shutdown.

Log Name: System
Source: Service Control Manager
Date: 12/13/2018 9:31:38 AM
Event ID: 7031
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: test10
Description:
The Adobe Acrobat Update Service service terminated unexpectedly. It has done this 1 time(s). The following corrective action will be taken in 60000 milliseconds: Restart the service.

I have checked my event log end it has event log like this, but service doesn’t start.

Killing Service Process Using Task Manager Details Section;

Log Name: System
Source: Service Control Manager
Date: 12/13/2018 17:55:22
Event ID: 7031
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: win10
Description:
The MyTest service terminated unexpectedly. It has done this 9 time(s). The following corrective action will be taken in 0 milliseconds: Restart the service .

Читайте также:  Драйвер запоминающего устройства для установки windows 10 64 bit

Killing Service Process Using Background Processes Section;

— No Log In Event Log —

Is the process really going away? Download and run Process Explorer from sysinternals.com (Microsoft). Find your service and note the process names and PID numbers. Use CTRL+T to show the process tree. Under the View menu set the update speed to 10 seconds. Then kill the service in the background processes section. Recheck procexp to verify that all processes terminated.

Does your service have its own logging function? Anything interesting in those logs?

Yes process is really going away because when i check from services.msc, service seems stopped.

My service has own logging function and there is nothing interesting.

I have opened Process Explorer and find my service process then killed with Process Explorer. My service restarted immediately.

But when i do this operation using Windows task manager’s background processes section, my service doesn’t restart and there is no eventlog in system.

I will do more research about it, maybe 0 minutes is not a good choice for service recovery. I will try something bigger than 0.

But when i do this operation using Windows task manager’s background processes section, my service doesn’t restart and there is no eventlog in system.

Did the processes terminate when you did it that way? Does your service log a startup event?

I will do more research about it, maybe 0 minutes is not a good choice for service recovery. I will try something bigger than 0.

I normally have it reset after 1 day and restart after at least 1 minute to give all process time to clean up.

>>I will do more research about it, maybe 0 minutes is not a good choice for service recovery. I will try something bigger than 0.

Yes, you get the point.

Look at this similar case:

Windows Services Recovery option doesn’t work . or I don’t understand it

to fix this, set the failure count to reset after one day. The drawback to this approach is your service may stay stopped after failing several times but this likely means something is toast anyways.

What’s more, Service Recovery only works for unexpected exit like (exit(-1)) call. For all the way we use to stop the service in usual way will not works for recovery. If you want to stop service and still wants recovery to work, call exit(-1) and you will see error message as «service stopped with unexpected error» , and then your service will restart as recovery setting is.

Please remember to mark the replies as answers if they help.
If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

I have read that thread but in my opinion my problem is different.

«The service control manager counts the number of times each service has failed since the system booted. The count is reset to 0 if the service has not failed for dwResetPeriod seconds. When the service fails for the N th time, the service controller performs the action specified in element [ N -1] of the lpsaActions array. If N is greater than cActions , the service controller repeats the last action in the array.»

Now The question is how a service fails? Or how service control manager decides a service has failed?

FailureActionsOnNonCrashFailures

«If this member is TRUE and the service has configured failure actions, the failure actions are queued if the service process terminates without reporting a status of SERVICE_STOPPED or if it enters the SERVICE_STOPPED state but the dwWin32ExitCode member of the SERVICE_STATUS structure is not ERROR_SUCCESS (0).

If this member is FALSE and the service has configured failure actions, the failure actions are queued only if the service terminates without reporting a status of SERVICE_STOPPED

Читайте также:  Windows boot manager как удалить лишний

As I understand if my service process is killed without stopping service, Service Control Manager will recover it if i set an action.

Everything seems good until this point;

If I Kill My Service Process with Process Explorer, or CMD taskkill command, Or Task Manager’s Details Tab everything is working as it should be.

But if I Kill My Service Process with Windows Task Manager’s Background Processes Section;

My Service doesnt restart;

I dont have any event log in system;

My question is; What is the difference between these?

Why my service doesn’t restart if i kill it with task manager, and why it restarts if i kill with process explorer?

I tried it with Print Spooler Service too. I killed Print Spooler Service via background processes section, service didnt restart. Nothing in event log.

I killed Print Spooler Service via Process Explorer, it restarted via Service Control Manager and I have a system log in event log.

Can you try it? My os is Windows 10 Pro — Version 1809

Script to set up «Auto-Restart After Failure» In Services

We have a service that is randomly stopping at multiple locations, we are still determining the cause but is there a way to turn on the option for «Restart After Failure» via a script of some kind?

I’d like to deploy it to a group of about 100 stores and see how it goes and then deploy it to the remaining 1400 stores.

This service stopping is an easy fix sure, but that’s so reactive I want to make sure we’re ahead of this.

You can do this with a batch file:

Which will configure to restart automatically after x seconds from the first failure, y seconds after the second failure, and z seconds after the third failure. This has to be ran as an administrator. Please note that is the name of the service (right click on the service and click Properties to get this), and NOT it’s display name.

Alternatively, you can configure it via GPO:

7 Replies

You can do this with a batch file:

Which will configure to restart automatically after x seconds from the first failure, y seconds after the second failure, and z seconds after the third failure. This has to be ran as an administrator. Please note that is the name of the service (right click on the service and click Properties to get this), and NOT it’s display name.

Alternatively, you can configure it via GPO:

I believe this is very close to what you are trying to accomplish:

Thank you both! This is why I LOVE Spiceworks community.

So is it supposed to look like this?

SC Failure actions= restart/ /restart/ /restart/ // reset=

Does this work on Windows 7? Forgive me, it’s been a WHILE since I’ve done scripting like this.

If you want the service CtlSvr to:

First failure: Restart the service after 60000ms (1 minute)

Second failure: Restart the service after 60000ms (1 minute)

Subsequent failures: Restart the service after 60000ms (1 minute)

Restart fail count after: 3600000s (41.667 days)

Then you’re close. Things with <> around them are parameters that don’t have <> in the actual script. Also, units should be left out (I was just specifying them so you could set them properly). So, in the end your script would look like this:

Оцените статью