README.txt 16 KB

  1. NSSM: The Non-Sucking Service Manager
  2. Version 2.21, 2013-11-24
  3. NSSM is a service helper program similar to srvany and cygrunsrv. It can
  4. start any application as an NT service and will restart the service if it
  5. fails for any reason.
  6. NSSM also has a graphical service installer and remover.
  7. Full documentation can be found online at
  9. Since version 2.0, the GUI can be bypassed by entering all appropriate
  10. options on the command line.
  11. Since version 2.1, NSSM can be compiled for x64 platforms.
  12. Thanks Benjamin Mayrargue.
  13. Since version 2.2, NSSM can be configured to take different actions
  14. based on the exit code of the managed application.
  15. Since version 2.3, NSSM logs to the Windows event log more elegantly.
  16. Since version 2.5, NSSM respects environment variables in its parameters.
  17. Since version 2.8, NSSM tries harder to shut down the managed application
  18. gracefully and throttles restart attempts if the application doesn't run
  19. for a minimum amount of time.
  20. Since version 2.11, NSSM respects srvany's AppEnvironment parameter.
  21. Since version 2.13, NSSM is translated into French.
  22. Thanks François-Régis Tardy.
  23. Since version 2.15, NSSM is translated into Italian.
  24. Thanks Riccardo Gusmeroli.
  25. Since version 2.17, NSSM can try to shut down console applications by
  26. simulating a Control-C keypress. If they have installed a handler routine
  27. they can clean up and shut down gracefully on receipt of the event.
  28. Since version 2.17, NSSM can redirect the managed application's I/O streams
  29. to an arbitrary path.
  30. Since version 2.18, NSSM can be configured to wait a user-specified amount
  31. of time for the application to exit when shutting down.
  32. Since version 2.19, many more service options can be configured with the
  33. GUI installer as well as via the registry.
  34. Since version 2.19, NSSM can add to the service's environment by setting
  35. AppEnvironmentExtra in place of or in addition to the srvany-compatible
  36. AppEnvironment.
  37. Since version 2.22, NSSM can rotate existing output files when redirecting I/O.
  38. Since version 2.22, NSSM can set service display name, description and startup
  39. type.
  40. Usage
  41. -----
  42. In the usage notes below, arguments to the program may be written in angle
  43. brackets and/or square brackets. <string> means you must insert the
  44. appropriate string and [<string>] means the string is optional. See the
  45. examples below...
  46. Installation using the GUI
  47. --------------------------
  48. To install a service, run
  49. nssm install <servicename>
  50. You will be prompted to enter the full path to the application you wish
  51. to run and any command line options to pass to that application.
  52. Use the system service manager (services.msc) to control advanced service
  53. properties such as startup method and desktop interaction. NSSM may
  54. support these options at a later time...
  55. Installation using the command line
  56. -----------------------------------
  57. To install a service, run
  58. nssm install <servicename> <application> [<options>]
  59. NSSM will then attempt to install a service which runs the named application
  60. with the given options (if you specified any).
  61. Don't forget to enclose paths in "quotes" if they contain spaces!
  62. If you want to include quotes in the options you will need to """quote""" the
  64. Managing the service
  65. --------------------
  66. NSSM will launch the application listed in the registry when you send it a
  67. start signal and will terminate it when you send a stop signal. So far, so
  68. much like srvany. But NSSM is the Non-Sucking service manager and can take
  69. action if/when the application dies.
  70. With no configuration from you, NSSM will try to restart itself if it notices
  71. that the application died but you didn't send it a stop signal. NSSM will
  72. keep trying, pausing between each attempt, until the service is successfully
  73. started or you send it a stop signal.
  74. NSSM will pause an increasingly longer time between subsequent restart attempts
  75. if the service fails to start in a timely manner, up to a maximum of four
  76. minutes. This is so it does not consume an excessive amount of CPU time trying
  77. to start a failed application over and over again. If you identify the cause
  78. of the failure and don't want to wait you can use the Windows service console
  79. (where the service will be shown in Paused state) to send a continue signal to
  80. NSSM and it will retry within a few seconds.
  81. By default, NSSM defines "a timely manner" to be within 1500 milliseconds.
  82. You can change the threshold for the service by setting the number of
  83. milliseconds as a REG_DWORD value in the registry at
  84. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppThrottle.
  85. NSSM will look in the registry under
  86. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppExit for
  87. string (REG_EXPAND_SZ) values corresponding to the exit code of the application.
  88. If the application exited with code 1, for instance, NSSM will look for a
  89. string value under AppExit called "1" or, if it does not find it, will
  90. fall back to the AppExit (Default) value. You can find out the exit code
  91. for the application by consulting the system event log. NSSM will log the
  92. exit code when the application exits.
  93. Based on the data found in the registry, NSSM will take one of three actions:
  94. If the value data is "Restart" NSSM will try to restart the application as
  95. described above. This is its default behaviour.
  96. If the value data is "Ignore" NSSM will not try to restart the application
  97. but will continue running itself. This emulates the (usually undesirable)
  98. behaviour of srvany. The Windows Services console would show the service
  99. as still running even though the application has exited.
  100. If the value data is "Exit" NSSM will exit gracefully. The Windows Services
  101. console would show the service as stopped. If you wish to provide
  102. finer-grained control over service recovery you should use this code and
  103. edit the failure action manually. Please note that Windows versions prior
  104. to Vista will not consider such an exit to be a failure. On older versions
  105. of Windows you should use "Suicide" instead.
  106. If the value data is "Suicide" NSSM will simulate a crash and exit without
  107. informing the service manager. This option should only be used for
  108. pre-Vista systems where you wish to apply a service recovery action. Note
  109. that if the monitored application exits with code 0, NSSM will only honour a
  110. request to suicide if you explicitly configure a registry key for exit code 0.
  111. If only the default action is set to Suicide NSSM will instead exit gracefully.
  112. Stopping the service
  113. --------------------
  114. When stopping a service NSSM will attempt several different methods of killing
  115. the monitored application, each of which can be disabled if necessary.
  116. First NSSM will attempt to generate a Control-C event and send it to the
  117. application's console. Batch scripts or console applications may intercept
  118. the event and shut themselves down gracefully. GUI applications do not have
  119. consoles and will not respond to this method.
  120. Secondly NSSM will enumerate all windows created by the application and send
  121. them a WM_CLOSE message, requesting a graceful exit.
  122. Thirdly NSSM will enumerate all threads created by the application and send
  123. them a WM_QUIT message, requesting a graceful exit. Not all applications'
  124. threads have message queues; those which do not will not respond to this
  125. method.
  126. Finally NSSM will call TerminateProcess() to request that the operating
  127. system forcibly terminate the application. TerminateProcess() cannot be
  128. trapped or ignored, so in most circumstances the application will be killed.
  129. However, there is no guarantee that it will have a chance to perform any
  130. tidyup operations before it exits.
  131. Any or all of the methods above may be disabled. NSSM will look for the
  132. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppStopMethodSkip
  133. registry value which should be of type REG_DWORD set to a bit field describing
  134. which methods should not be applied.
  135. If AppStopMethodSkip includes 1, Control-C events will not be generated.
  136. If AppStopMethodSkip includes 2, WM_CLOSE messages will not be posted.
  137. If AppStopMethodSkip includes 4, WM_QUIT messages will not be posted.
  138. If AppStopMethodSkip includes 8, TerminateProcess() will not be called.
  139. If, for example, you knew that an application did not respond to Control-C
  140. events and did not have a thread message queue, you could set AppStopMethodSkip
  141. to 5 and NSSM would not attempt to use those methods to stop the application.
  142. Take great care when including 8 in the value of AppStopMethodSkip. If NSSM
  143. does not call TerminateProcess() it is possible that the application will not
  144. exit when the service stops.
  145. By default NSSM will allow processes 1500ms to respond to each of the methods
  146. described above before proceeding to the next one. The timeout can be
  147. configured on a per-method basis by creating REG_DWORD entries in the
  148. registry under HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters.
  149. AppStopMethodConsole
  150. AppStopMethodWindow
  151. AppStopMethodThreads
  152. Each value should be set to the number of milliseconds to wait. Please note
  153. that the timeout applies to each process in the application's process tree,
  154. so the actual time to shutdown may be longer than the sum of all configured
  155. timeouts if the application spawns multiple subprocesses.
  156. I/O redirection
  157. ---------------
  158. NSSM can redirect the managed application's I/O to any path capable of being
  159. opened by CreateFile(). This enables, for example, capturing the log output
  160. of an application which would otherwise only write to the console or accepting
  161. input from a serial port.
  162. NSSM will look in the registry under
  163. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters for the keys
  164. corresponding to arguments to CreateFile(). All are optional. If no path is
  165. given for a particular stream it will not be redirected. If a path is given
  166. but any of the other values are omitted they will be receive sensible defaults.
  167. AppStdin: Path to receive input.
  168. AppStdout: Path to receive output.
  169. AppStderr: Path to receive error output.
  170. Parameters for CreateFile() are providing with the "AppStdinShareMode",
  171. "AppStdinCreationDisposition" and "AppStdinFlagsAndAttributes" values (and
  172. analogously for stdout and stderr).
  173. In general, if you want the service to log its output, set AppStdout and
  174. AppStderr to the same path, eg C:\Users\Public\service.log, and it should
  175. work. Remember, however, that the path must be accessible to the user
  176. running the service.
  177. File rotation
  178. -------------
  179. When using I/O redirection, NSSM can rotate existing output files prior to
  180. opening stdout and/or stderr. An existing file will be renamed with a
  181. suffix based on the file's last write time, to millisecond precision. For
  182. example, the file nssm.log might be rotated to nssm-20131221T113939.457.log.
  183. NSSM will look in the registry under
  184. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters for REG_DWORD
  185. entries which control how rotation happens.
  186. If AppRotateFiles is missing or set to 0, rotation is disabled. Any non-zero
  187. value enables rotation.
  188. If AppRotateSeconds is non-zero, a file will not be rotated if its last write
  189. time is less than the given number of seconds in the past.
  190. If AppRotateBytes is non-zero, a file will not be rotated if it is smaller
  191. than the given number of bytes. 64-bit file sizes can be handled by setting
  192. a non-zero value of AppRotateBytesHigh.
  193. Rotation is independent of the CreateFile() parameters used to open the files.
  194. They will be rotated regardless of whether NSSM would otherwise have appended
  195. or replaced them.
  196. Environment variables
  197. ---------------------
  198. NSSM can replace or append to the managed application's environment. Two
  199. multi-valued string (REG_MULTI_SZ) registry values are recognised under
  200. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters.
  201. AppEnvironment defines a list of environment variables which will override
  202. the service's environment. AppEnvironmentExtra defines a list of
  203. environment variables which will be added to the service's environment.
  204. Each entry in the list should be of the form KEY=VALUE. It is possible to
  205. omit the VALUE but the = symbol is mandatory.
  206. srvany only supports AppEnvironment.
  207. Removing services using the GUI
  208. -------------------------------
  209. NSSM can also remove services. Run
  210. nssm remove <servicename>
  211. to remove a service. You will prompted for confirmation before the service
  212. is removed. Try not to remove essential system services...
  213. Removing service using the command line
  214. ---------------------------------------
  215. To remove a service without confirmation from the GUI, run
  216. nssm remove <servicename> confirm
  217. Try not to remove essential system services...
  218. Logging
  219. -------
  220. NSSM logs to the Windows event log. It registers itself as an event log source
  221. and uses unique event IDs for each type of message it logs. New versions may
  222. add event types but existing event IDs will never be changed.
  223. Because of the way NSSM registers itself you should be aware that you may not
  224. be able to replace the NSSM binary if you have the event viewer open and that
  225. running multiple instances of NSSM from different locations may be confusing if
  226. they are not all the same version.
  227. Example usage
  228. -------------
  229. To install an Unreal Tournament server:
  230. nssm install UT2004 c:\games\ut2004\system\ucc.exe server
  231. To remove the server:
  232. nssm remove UT2004 confirm
  233. Building NSSM from source
  234. -------------------------
  235. NSSM is known to compile with Visual Studio 2008. Older Visual Studio
  236. releases may or may not work.
  237. NSSM will also compile with Visual Studio 2010 but the resulting executable
  238. will not run on versions of Windows older than XP SP2. If you require
  239. compatiblity with older Windows releases you should change the Platform
  240. Toolset to v90 in the General section of the project's Configuration
  241. Properties.
  242. Credits
  243. -------
  244. Thanks to Bernard Loh for finding a bug with service recovery.
  245. Thanks to Benjamin Mayrargue ( for adding 64-bit support.
  246. Thanks to Joel Reingold for spotting a command line truncation bug.
  247. Thanks to Arve Knudsen for spotting that child processes of the monitored
  248. application could be left running on service shutdown, and that a missing
  249. registry value for AppDirectory confused NSSM.
  250. Thanks to Peter Wagemans and Laszlo Keresztfalvi for suggesting throttling restarts.
  251. Thanks to Eugene Lifshitz for finding an edge case in CreateProcess() and for
  252. advising how to build correctly in paths containing spaces.
  253. Thanks to Rob Sharp for pointing out that NSSM did not respect the
  254. AppEnvironment registry value used by srvany.
  255. Thanks to Szymon Nowak for help with Windows 2000 compatibility.
  256. Thanks to François-Régis Tardy for French translation.
  257. Thanks to Emilio Frini for spotting that French was inadvertently set as
  258. the default language when the user's display language was not translated.
  259. Thanks to Riccardo Gusmeroli for Italian translation.
  260. Thanks to Eric Cheldelin for the inspiration to generate a Control-C event
  261. on shutdown.
  262. Thanks to Brian Baxter for suggesting how to escape quotes from the command prompt.
  263. Thanks to Russ Holmann for suggesting that the shutdown timeout be configurable.
  264. Thanks to Paul Spause for spotting a bug with default registry entries.
  265. Thanks to BUGHUNTER for spotting more GUI bugs.
  266. Thanks to Doug Watson for suggesting file rotation.
  267. Licence
  268. -------
  269. NSSM is public domain. You may unconditionally use it and/or its source code
  270. for any purpose you wish.