Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create service which restarts on crash

I am creating a service using CreateService. The service will run again fine if it happens to crash and I would like to have Windows restart the service if it crashes. I know it is possible to set this up from the services msc see below.

Windows Service Recovery Dialog

How can I programatically configure the service to always restart if it happens to crash.

like image 394
JProgrammer Avatar asked Jul 14 '10 00:07

JProgrammer


1 Answers

Used Deltanine's approach, but modified it a bit to be able to control each failure action:

SERVICE_FAILURE_ACTIONS servFailActions;
SC_ACTION failActions[3];

failActions[0].Type = SC_ACTION_RESTART; //Failure action: Restart Service
failActions[0].Delay = 120000; //number of seconds to wait before performing failure action, in milliseconds = 2minutes
failActions[1].Type = SC_ACTION_RESTART;
failActions[1].Delay = 120000;
failActions[2].Type = SC_ACTION_NONE;
failActions[2].Delay = 120000;

servFailActions.dwResetPeriod = 86400; // Reset Failures Counter, in Seconds = 1day
servFailActions.lpCommand = NULL; //Command to perform due to service failure, not used
servFailActions.lpRebootMsg = NULL; //Message during rebooting computer due to service failure, not used
servFailActions.cActions = 3; // Number of failure action to manage
servFailActions.lpsaActions = failActions;

ChangeServiceConfig2(sc_service, SERVICE_CONFIG_FAILURE_ACTIONS, &servFailActions); //Apply above settings
like image 114
MarcusUA Avatar answered Sep 27 '22 21:09

MarcusUA