22

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?

M Schenkel
  • 6,294
  • 12
  • 62
  • 107
  • This question may help http://stackoverflow.com/questions/220382/how-can-a-windows-service-programmatically-restart-itself?rq=1 – tom redfern Oct 10 '14 at 15:51

4 Answers4

24

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.

Larry Osterman
  • 16,086
  • 32
  • 60
  • 2
    Thanks. This shed some light on the situation. I found when I did a "End Process" from taskmgr it produces this in the event viewer: The following corrective action will be taken in 60000 milliseconds: Restart the service. Now my problem (which is related to my coding) is to have it terminiate "Un-Gracefully" so that the recovery logic will kick in. – M Schenkel Nov 19 '09 at 21:07
  • 1
    If you call ExitProcess that should be sufficient. – Larry Osterman Nov 20 '09 at 06:29
13

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.

4

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...

Len Holgate
  • 21,282
  • 4
  • 45
  • 92
2

If you 'kill' service from task manager - forgot for recovery logic. In background task manager 'kills' process by 'stop service'. and as yuo can guess - this is not service failure. This forced me to kill it really with Visual Studio. In task manager right click on service process. Select debug. In Visual studio select Debug-> Terminate All. And now you have simulated service fail. In this case recovery logic works fine.

Kostadin
  • 2,499
  • 5
  • 34
  • 58