\r
With no configuration from you, NSSM will try to restart itself if it notices\r
that the application died but you didn't send it a stop signal. NSSM will\r
-keep trying, pausing 30 seconds between each attempt, until the service is\r
-successfully started or you send it a stop signal.\r
+keep trying, pausing between each attempt, until the service is successfully\r
+started or you send it a stop signal.\r
+\r
+NSSM will pause an increasingly longer time between subsequent restart attempts\r
+if the service fails to start in a timely manner, up to a maximum of 60 seconds.\r
+This is so it does not consume an excessive amount of CPU time trying to start\r
+a failed application over and over again. If you identify the cause of the\r
+failure and don't want to wait you can use the Windows service console to\r
+send a continue signal to NSSM and it will retry within a few seconds.\r
\r
NSSM will look in the registry under\r
HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppExit for\r
Thanks to Arve Knudsen for spotting that child processes of the monitored\r
application could be left running on service shutdown, and that a missing\r
registry value for AppDirectory confused NSSM.\r
+Thanks to Peter Wagemans and Laszlo Kereszt for suggesting throttling restarts.\r
\r
Licence\r
-------\r
char flags[CMD_LENGTH];\r
char dir[MAX_PATH];\r
bool stopping;\r
+CRITICAL_SECTION throttle_section;\r
+CONDITION_VARIABLE throttle_condition;\r
\r
static enum { NSSM_EXIT_RESTART, NSSM_EXIT_IGNORE, NSSM_EXIT_REALLY, NSSM_EXIT_UNCLEAN } exit_actions;\r
static const char *exit_action_strings[] = { "Restart", "Ignore", "Exit", "Suicide", 0 };\r
\r
+static unsigned long throttle;\r
+\r
+static inline int throttle_milliseconds() {\r
+ /* pow() operates on doubles. */\r
+ int ret = 1; for (unsigned long i = 1; i < throttle; i++) ret *= 2;\r
+ return ret * 1000;\r
+}\r
+\r
/* Connect to the service manager */\r
SC_HANDLE open_service_manager() {\r
SC_HANDLE ret = OpenSCManager(0, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);\r
/* Initialise status */\r
ZeroMemory(&service_status, sizeof(service_status));\r
service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;\r
- service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;\r
+ service_status.dwControlsAccepted = SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE;\r
service_status.dwWin32ExitCode = NO_ERROR;\r
service_status.dwServiceSpecificExitCode = 0;\r
service_status.dwCheckPoint = 0;\r
- service_status.dwWaitHint = 1000;\r
+ service_status.dwWaitHint = NSSM_WAITHINT_MARGIN;\r
\r
/* Signal we AREN'T running the server */\r
process_handle = 0;\r
}\r
\r
service_status.dwCurrentState = SERVICE_START_PENDING;\r
+ service_status.dwWaitHint = NSSM_RESET_THROTTLE_RESTART + NSSM_WAITHINT_MARGIN;\r
SetServiceStatus(service_handle, &service_status);\r
\r
/* Try to create the exit action parameters; we don't care if it fails */\r
\r
set_service_recovery(service_name);\r
\r
+ /* Used for signalling a resume if the service pauses when throttled. */\r
+ InitializeCriticalSection(&throttle_section);\r
+\r
monitor_service();\r
}\r
\r
case SERVICE_CONTROL_STOP:\r
stop_service(0, true, true);\r
return NO_ERROR;\r
+\r
+ case SERVICE_CONTROL_CONTINUE:\r
+ throttle = 0;\r
+ WakeConditionVariable(&throttle_condition);\r
+ service_status.dwCurrentState = SERVICE_CONTINUE_PENDING;\r
+ service_status.dwWaitHint = throttle_milliseconds() + NSSM_WAITHINT_MARGIN;\r
+ log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_RESET_THROTTLE, service_name, 0);\r
+ SetServiceStatus(service_handle, &service_status);\r
+ return NO_ERROR;\r
+\r
+ case SERVICE_CONTROL_PAUSE:\r
+ /*\r
+ We don't accept pause messages but it isn't possible to register\r
+ only for continue messages so we have to handle this case.\r
+ */\r
+ return ERROR_CALL_NOT_IMPLEMENTED;\r
}\r
\r
/* Unknown control */\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, "command line", "start_service", 0);\r
return stop_service(2, true, true);\r
}\r
+\r
+ throttle_restart();\r
+\r
if (! CreateProcess(0, cmd, 0, 0, false, 0, 0, dir, &si, &pi)) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service_name, exe, GetLastError(), 0);\r
return stop_service(3, true, true);\r
service_status.dwCurrentState = SERVICE_RUNNING;\r
SetServiceStatus(service_handle, &service_status);\r
\r
+ /* Wait for a clean startup. */\r
+ if (WaitForSingleObject(process_handle, NSSM_RESET_THROTTLE_RESTART) == WAIT_TIMEOUT) throttle = 0;\r
+\r
return 0;\r
}\r
\r
/* Signal we are stopping */\r
if (graceful) {\r
service_status.dwCurrentState = SERVICE_STOP_PENDING;\r
+ service_status.dwWaitHint = NSSM_KILL_WINDOW_GRACE_PERIOD + NSSM_KILL_THREADS_GRACE_PERIOD + NSSM_WAITHINT_MARGIN;\r
SetServiceStatus(service_handle, &service_status);\r
}\r
\r
break;\r
}\r
}\r
+\r
+void throttle_restart() {\r
+ /* This can't be a restart if the service is already running. */\r
+ if (! throttle++) return;\r
+\r
+ int ms = throttle_milliseconds();\r
+\r
+ if (throttle > 7) throttle = 8;\r
+\r
+ char threshold[8], milliseconds[8];\r
+ _snprintf(threshold, sizeof(threshold), "%d", NSSM_RESET_THROTTLE_RESTART);\r
+ _snprintf(milliseconds, sizeof(milliseconds), "%d", ms);\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_THROTTLED, service_name, threshold, milliseconds, 0);\r
+\r
+ EnterCriticalSection(&throttle_section);\r
+\r
+ service_status.dwCurrentState = SERVICE_PAUSED;\r
+ SetServiceStatus(service_handle, &service_status);\r
+\r
+ SleepConditionVariableCS(&throttle_condition, &throttle_section, ms);\r
+\r
+ LeaveCriticalSection(&throttle_section);\r
+}\r