README.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. NSSM: The Non-Sucking Service Manager
  2. Version 2.16, 2012-12-01
  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
  8. http://nssm.cc/
  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. Usage
  31. -----
  32. In the usage notes below, arguments to the program may be written in angle
  33. brackets and/or square brackets. <string> means you must insert the
  34. appropriate string and [<string>] means the string is optional. See the
  35. examples below...
  36. Installation using the GUI
  37. --------------------------
  38. To install a service, run
  39. nssm install <servicename>
  40. You will be prompted to enter the full path to the application you wish
  41. to run and any command line options to pass to that application.
  42. Use the system service manager (services.msc) to control advanced service
  43. properties such as startup method and desktop interaction. NSSM may
  44. support these options at a later time...
  45. Installation using the command line
  46. -----------------------------------
  47. To install a service, run
  48. nssm install <servicename> <application> [<options>]
  49. NSSM will then attempt to install a service which runs the named application
  50. with the given options (if you specified any).
  51. Don't forget to enclose paths in "quotes" if they contain spaces!
  52. If you want to include quotes in the options you will need to """quote""" the
  53. quotes.
  54. Managing the service
  55. --------------------
  56. NSSM will launch the application listed in the registry when you send it a
  57. start signal and will terminate it when you send a stop signal. So far, so
  58. much like srvany. But NSSM is the Non-Sucking service manager and can take
  59. action if/when the application dies.
  60. With no configuration from you, NSSM will try to restart itself if it notices
  61. that the application died but you didn't send it a stop signal. NSSM will
  62. keep trying, pausing between each attempt, until the service is successfully
  63. started or you send it a stop signal.
  64. NSSM will pause an increasingly longer time between subsequent restart attempts
  65. if the service fails to start in a timely manner, up to a maximum of four
  66. minutes. This is so it does not consume an excessive amount of CPU time trying
  67. to start a failed application over and over again. If you identify the cause
  68. of the failure and don't want to wait you can use the Windows service console
  69. (where the service will be shown in Paused state) to send a continue signal to
  70. NSSM and it will retry within a few seconds.
  71. By default, NSSM defines "a timely manner" to be within 1500 milliseconds.
  72. You can change the threshold for the service by setting the number of
  73. milliseconds as a REG_DWORD value in the registry at
  74. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppThrottle.
  75. NSSM will look in the registry under
  76. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppExit for
  77. string (REG_EXPAND_SZ) values corresponding to the exit code of the application.
  78. If the application exited with code 1, for instance, NSSM will look for a
  79. string value under AppExit called "1" or, if it does not find it, will
  80. fall back to the AppExit (Default) value. You can find out the exit code
  81. for the application by consulting the system event log. NSSM will log the
  82. exit code when the application exits.
  83. Based on the data found in the registry, NSSM will take one of three actions:
  84. If the value data is "Restart" NSSM will try to restart the application as
  85. described above. This is its default behaviour.
  86. If the value data is "Ignore" NSSM will not try to restart the application
  87. but will continue running itself. This emulates the (usually undesirable)
  88. behaviour of srvany. The Windows Services console would show the service
  89. as still running even though the application has exited.
  90. If the value data is "Exit" NSSM will exit gracefully. The Windows Services
  91. console would show the service as stopped. If you wish to provide
  92. finer-grained control over service recovery you should use this code and
  93. edit the failure action manually. Please note that Windows versions prior
  94. to Vista will not consider such an exit to be a failure. On older versions
  95. of Windows you should use "Suicide" instead.
  96. If the value data is "Suicide" NSSM will simulate a crash and exit without
  97. informing the service manager. This option should only be used for
  98. pre-Vista systems where you wish to apply a service recovery action. Note
  99. that if the monitored application exits with code 0, NSSM will only honour a
  100. request to suicide if you explicitly configure a registry key for exit code 0.
  101. If only the default action is set to Suicide NSSM will instead exit gracefully.
  102. Stopping the service
  103. --------------------
  104. When stopping a service NSSM will attempt several different methods of killing
  105. the monitored application, each of which can be disabled if necessary.
  106. First NSSM will attempt to generate a Control-C event and send it to the
  107. application's console. Batch scripts or console applications may intercept
  108. the event and shut themselves down gracefully. GUI applications do not have
  109. consoles and will not respond to this method.
  110. Secondly NSSM will enumerate all windows created by the application and send
  111. them a WM_CLOSE message, requesting a graceful exit.
  112. Thirdly NSSM will enumerate all threads created by the application and send
  113. them a WM_QUIT message, requesting a graceful exit. Not all applications'
  114. threads have message queues; those which do not will not respond to this
  115. method.
  116. Finally NSSM will call TerminateProcess() to request that the operating
  117. system forcibly terminate the application. TerminateProcess() cannot be
  118. trapped or ignored, so in most circumstances the application will be killed.
  119. However, there is no guarantee that it will have a chance to perform any
  120. tidyup operations before it exits.
  121. Any or all of the methods above may be disabled. NSSM will look for the
  122. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppStopMethodSkip
  123. registry value which should be of type REG_DWORD set to a bit field describing
  124. which methods should not be applied.
  125. If AppStopMethodSkip includes 1, Control-C events will not be generated.
  126. If AppStopMethodSkip includes 2, WM_CLOSE messages will not be posted.
  127. If AppStopMethodSkip includes 4, WM_QUIT messages will not be posted.
  128. If AppStopMethodSkip includes 8, TerminateProcess() will not be called.
  129. If, for example, you knew that an application did not respond to Control-C
  130. events and did not have a thread message queue, you could set AppStopMethodSkip
  131. to 5 and NSSM would not attempt to use those methods to stop the application.
  132. Take great care when including 8 in the value of AppStopMethodSkip. If NSSM
  133. does not call TerminateProcess() it is possible that the application will not
  134. exit when the service stops.
  135. I/O redirection
  136. ---------------
  137. NSSM can redirect the managed application's I/O to any path capable of being
  138. opened by CreateFile(). This enables, for example, capturing the log output
  139. of an application which would otherwise only write to the console or accepting
  140. input from a serial port.
  141. NSSM will look in the registry under
  142. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters for the keys
  143. corresponding to arguments to CreateFile(). All are optional. If no path is
  144. given for a particular stream it will not be redirected. If a path is given
  145. but any of the other values are omitted they will be receive sensible defaults.
  146. AppStdin: Path to receive input.
  147. AppStdout: Path to receive output.
  148. AppStderr: Path to receive error output.
  149. Parameters for CreateFile() are providing with the "AppStdinShareMode",
  150. "AppStdinCreationDisposition" and "AppStdinFlagsAndAttributes" values (and
  151. analogously for stdout and stderr).
  152. In general, if you want the service to log its output, set AppStdout and
  153. AppStderr to the same path, eg C:\Users\Public\service.log, and it should
  154. work. Remember, however, that the path must be accessible to the user
  155. running the service.
  156. Removing services using the GUI
  157. -------------------------------
  158. NSSM can also remove services. Run
  159. nssm remove <servicename>
  160. to remove a service. You will prompted for confirmation before the service
  161. is removed. Try not to remove essential system services...
  162. Removing service using the command line
  163. ---------------------------------------
  164. To remove a service without confirmation from the GUI, run
  165. nssm remove <servicename> confirm
  166. Try not to remove essential system services...
  167. Logging
  168. -------
  169. NSSM logs to the Windows event log. It registers itself as an event log source
  170. and uses unique event IDs for each type of message it logs. New versions may
  171. add event types but existing event IDs will never be changed.
  172. Because of the way NSSM registers itself you should be aware that you may not
  173. be able to replace the NSSM binary if you have the event viewer open and that
  174. running multiple instances of NSSM from different locations may be confusing if
  175. they are not all the same version.
  176. Example usage
  177. -------------
  178. To install an Unreal Tournament server:
  179. nssm install UT2004 c:\games\ut2004\system\ucc.exe server
  180. To remove the server:
  181. nssm remove UT2004 confirm
  182. Building NSSM from source
  183. -------------------------
  184. NSSM is known to compile with Visual Studio 6, Visual Studio 2005 and Visual
  185. Studio 2008.
  186. NSSM will also compile with Visual Studio 2010 but the resulting executable
  187. will not run on versions of Windows older than XP SP2.
  188. Credits
  189. -------
  190. Thanks to Bernard Loh for finding a bug with service recovery.
  191. Thanks to Benjamin Mayrargue (www.softlion.com) for adding 64-bit support.
  192. Thanks to Joel Reingold for spotting a command line truncation bug.
  193. Thanks to Arve Knudsen for spotting that child processes of the monitored
  194. application could be left running on service shutdown, and that a missing
  195. registry value for AppDirectory confused NSSM.
  196. Thanks to Peter Wagemans and Laszlo Keresztfalvi for suggesting throttling restarts.
  197. Thanks to Eugene Lifshitz for finding an edge case in CreateProcess() and for
  198. advising how to build messages.mc correctly in paths containing spaces.
  199. Thanks to Rob Sharp for pointing out that NSSM did not respect the
  200. AppEnvironment registry value used by srvany.
  201. Thanks to Szymon Nowak for help with Windows 2000 compatibility.
  202. Thanks to François-Régis Tardy for French translation.
  203. Thanks to Emilio Frini for spotting that French was inadvertently set as
  204. the default language when the user's display language was not translated.
  205. Thanks to Riccardo Gusmeroli for Italian translation.
  206. Thanks to Eric Cheldelin for the inspiration to generate a Control-C event
  207. on shutdown.
  208. Thanks to Brian Baxter for suggesting how to escape quotes from the command prompt.
  209. Licence
  210. -------
  211. NSSM is public domain. You may unconditionally use it and/or its source code
  212. for any purpose you wish.