README.txt 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. NSSM: The Non-Sucking Service Manager
  2. Version 2.24, 2014-08-31
  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. 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 set the managed application's process priority
  38. and CPU affinity.
  39. Since version 2.22, NSSM can apply an unconditional delay before restarting
  40. an application which has exited.
  41. Since version 2.22, NSSM can rotate existing output files when redirecting I/O.
  42. Since version 2.22, NSSM can set service display name, description, startup
  43. type, log on details and dependencies.
  44. Since version 2.22, NSSM can manage existing services.
  45. Since version 2.25, NSSM can execute commands in response to service events.
  46. Since version 2.25, NSSM can list services it manages.
  47. Since version 2.25, NSSM can dump the configuration of services it manages.
  48. Since version 2.25, NSSM can show the processes managed by a service.
  49. Usage
  50. -----
  51. In the usage notes below, arguments to the program may be written in angle
  52. brackets and/or square brackets. <string> means you must insert the
  53. appropriate string and [<string>] means the string is optional. See the
  54. examples below...
  55. Note that everywhere <servicename> appears you may substitute the
  56. service's display name.
  57. Installation using the GUI
  58. --------------------------
  59. To install a service, run
  60. nssm install <servicename>
  61. You will be prompted to enter the full path to the application you wish
  62. to run and any command line options to pass to that application.
  63. Use the system service manager (services.msc) to control advanced service
  64. properties such as startup method and desktop interaction. NSSM may
  65. support these options at a later time...
  66. Installation using the command line
  67. -----------------------------------
  68. To install a service, run
  69. nssm install <servicename> <application> [<options>]
  70. NSSM will then attempt to install a service which runs the named application
  71. with the given options (if you specified any).
  72. Don't forget to enclose paths in "quotes" if they contain spaces!
  73. If you want to include quotes in the options you will need to """quote""" the
  74. quotes.
  75. Managing the service
  76. --------------------
  77. NSSM will launch the application listed in the registry when you send it a
  78. start signal and will terminate it when you send a stop signal. So far, so
  79. much like srvany. But NSSM is the Non-Sucking service manager and can take
  80. action if/when the application dies.
  81. With no configuration from you, NSSM will try to restart itself if it notices
  82. that the application died but you didn't send it a stop signal. NSSM will
  83. keep trying, pausing between each attempt, until the service is successfully
  84. started or you send it a stop signal.
  85. NSSM will pause an increasingly longer time between subsequent restart attempts
  86. if the service fails to start in a timely manner, up to a maximum of four
  87. minutes. This is so it does not consume an excessive amount of CPU time trying
  88. to start a failed application over and over again. If you identify the cause
  89. of the failure and don't want to wait you can use the Windows service console
  90. (where the service will be shown in Paused state) to send a continue signal to
  91. NSSM and it will retry within a few seconds.
  92. By default, NSSM defines "a timely manner" to be within 1500 milliseconds.
  93. You can change the threshold for the service by setting the number of
  94. milliseconds as a REG_DWORD value in the registry at
  95. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppThrottle.
  96. Alternatively, NSSM can pause for a configurable amount of time before
  97. attempting to restart the application even if it successfully ran for the
  98. amount of time specified by AppThrottle. NSSM will consult the REG_DWORD value
  99. at HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppRestartDelay
  100. for the number of milliseconds to wait before attempting a restart. If
  101. AppRestartDelay is set and the application is determined to be subject to
  102. throttling, NSSM will pause the service for whichever is longer of the
  103. configured restart delay and the calculated throttle period.
  104. If AppRestartDelay is missing or invalid, only throttling will be applied.
  105. NSSM will look in the registry under
  106. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppExit for
  107. string (REG_EXPAND_SZ) values corresponding to the exit code of the application.
  108. If the application exited with code 1, for instance, NSSM will look for a
  109. string value under AppExit called "1" or, if it does not find it, will
  110. fall back to the AppExit (Default) value. You can find out the exit code
  111. for the application by consulting the system event log. NSSM will log the
  112. exit code when the application exits.
  113. Based on the data found in the registry, NSSM will take one of three actions:
  114. If the value data is "Restart" NSSM will try to restart the application as
  115. described above. This is its default behaviour.
  116. If the value data is "Ignore" NSSM will not try to restart the application
  117. but will continue running itself. This emulates the (usually undesirable)
  118. behaviour of srvany. The Windows Services console would show the service
  119. as still running even though the application has exited.
  120. If the value data is "Exit" NSSM will exit gracefully. The Windows Services
  121. console would show the service as stopped. If you wish to provide
  122. finer-grained control over service recovery you should use this code and
  123. edit the failure action manually. Please note that Windows versions prior
  124. to Vista will not consider such an exit to be a failure. On older versions
  125. of Windows you should use "Suicide" instead.
  126. If the value data is "Suicide" NSSM will simulate a crash and exit without
  127. informing the service manager. This option should only be used for
  128. pre-Vista systems where you wish to apply a service recovery action. Note
  129. that if the monitored application exits with code 0, NSSM will only honour a
  130. request to suicide if you explicitly configure a registry key for exit code 0.
  131. If only the default action is set to Suicide NSSM will instead exit gracefully.
  132. Application priority
  133. --------------------
  134. NSSM can set the priority class of the managed application. NSSM will look in
  135. the registry under HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters
  136. for the REG_DWORD entry AppPriority. Valid values correspond to arguments to
  137. SetPriorityClass(). If AppPriority() is missing or invalid the
  138. application will be launched with normal priority.
  139. Processor affinity
  140. ------------------
  141. NSSM can set the CPU affinity of the managed application. NSSM will look in
  142. the registry under HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters
  143. for the REG_SZ entry AppAffinity. It should specify a comma-separated listed
  144. of zero-indexed processor IDs. A range of processors may optionally be
  145. specified with a dash. No other characters are allowed in the string.
  146. For example, to specify the first; second; third and fifth CPUs, an appropriate
  147. AppAffinity would be 0-2,4.
  148. If AppAffinity is missing or invalid, NSSM will not attempt to restrict the
  149. application to specific CPUs.
  150. Note that the 64-bit version of NSSM can configure a maximum of 64 CPUs in this
  151. way and that the 32-bit version can configure a maxium of 32 CPUs even when
  152. running on 64-bit Windows.
  153. Stopping the service
  154. --------------------
  155. When stopping a service NSSM will attempt several different methods of killing
  156. the monitored application, each of which can be disabled if necessary.
  157. First NSSM will attempt to generate a Control-C event and send it to the
  158. application's console. Batch scripts or console applications may intercept
  159. the event and shut themselves down gracefully. GUI applications do not have
  160. consoles and will not respond to this method.
  161. Secondly NSSM will enumerate all windows created by the application and send
  162. them a WM_CLOSE message, requesting a graceful exit.
  163. Thirdly NSSM will enumerate all threads created by the application and send
  164. them a WM_QUIT message, requesting a graceful exit. Not all applications'
  165. threads have message queues; those which do not will not respond to this
  166. method.
  167. Finally NSSM will call TerminateProcess() to request that the operating
  168. system forcibly terminate the application. TerminateProcess() cannot be
  169. trapped or ignored, so in most circumstances the application will be killed.
  170. However, there is no guarantee that it will have a chance to perform any
  171. tidyup operations before it exits.
  172. Any or all of the methods above may be disabled. NSSM will look for the
  173. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppStopMethodSkip
  174. registry value which should be of type REG_DWORD set to a bit field describing
  175. which methods should not be applied.
  176. If AppStopMethodSkip includes 1, Control-C events will not be generated.
  177. If AppStopMethodSkip includes 2, WM_CLOSE messages will not be posted.
  178. If AppStopMethodSkip includes 4, WM_QUIT messages will not be posted.
  179. If AppStopMethodSkip includes 8, TerminateProcess() will not be called.
  180. If, for example, you knew that an application did not respond to Control-C
  181. events and did not have a thread message queue, you could set AppStopMethodSkip
  182. to 5 and NSSM would not attempt to use those methods to stop the application.
  183. Take great care when including 8 in the value of AppStopMethodSkip. If NSSM
  184. does not call TerminateProcess() it is possible that the application will not
  185. exit when the service stops.
  186. By default NSSM will allow processes 1500ms to respond to each of the methods
  187. described above before proceeding to the next one. The timeout can be
  188. configured on a per-method basis by creating REG_DWORD entries in the
  189. registry under HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters.
  190. AppStopMethodConsole
  191. AppStopMethodWindow
  192. AppStopMethodThreads
  193. Each value should be set to the number of milliseconds to wait. Please note
  194. that the timeout applies to each process in the application's process tree,
  195. so the actual time to shutdown may be longer than the sum of all configured
  196. timeouts if the application spawns multiple subprocesses.
  197. To skip applying the above stop methods to all processes in the application's
  198. process tree, applying them only to the original application process, set the
  199. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppKillProcessTree
  200. registry value, which should be of type REG_DWORD, to 0.
  201. Console window
  202. --------------
  203. By default, NSSM will create a console window so that applications which
  204. are capable of reading user input can do so - subject to the service being
  205. allowed to interact with the desktop.
  206. Creation of the console can be suppressed by setting the integer (REG_DWORD)
  207. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppNoConsole
  208. registry value to 1.
  209. I/O redirection
  210. ---------------
  211. NSSM can redirect the managed application's I/O to any path capable of being
  212. opened by CreateFile(). This enables, for example, capturing the log output
  213. of an application which would otherwise only write to the console or accepting
  214. input from a serial port.
  215. NSSM will look in the registry under
  216. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters for the keys
  217. corresponding to arguments to CreateFile(). All are optional. If no path is
  218. given for a particular stream it will not be redirected. If a path is given
  219. but any of the other values are omitted they will be receive sensible defaults.
  220. AppStdin: Path to receive input.
  221. AppStdout: Path to receive output.
  222. AppStderr: Path to receive error output.
  223. Parameters for CreateFile() are providing with the "AppStdinShareMode",
  224. "AppStdinCreationDisposition" and "AppStdinFlagsAndAttributes" values (and
  225. analogously for stdout and stderr).
  226. In general, if you want the service to log its output, set AppStdout and
  227. AppStderr to the same path, eg C:\Users\Public\service.log, and it should
  228. work. Remember, however, that the path must be accessible to the user
  229. running the service.
  230. File rotation
  231. -------------
  232. When using I/O redirection, NSSM can rotate existing output files prior to
  233. opening stdout and/or stderr. An existing file will be renamed with a
  234. suffix based on the file's last write time, to millisecond precision. For
  235. example, the file nssm.log might be rotated to nssm-20131221T113939.457.log.
  236. NSSM will look in the registry under
  237. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters for REG_DWORD
  238. entries which control how rotation happens.
  239. If AppRotateFiles is missing or set to 0, rotation is disabled. Any non-zero
  240. value enables rotation.
  241. If AppRotateSeconds is non-zero, a file will not be rotated if its last write
  242. time is less than the given number of seconds in the past.
  243. If AppRotateBytes is non-zero, a file will not be rotated if it is smaller
  244. than the given number of bytes. 64-bit file sizes can be handled by setting
  245. a non-zero value of AppRotateBytesHigh.
  246. If AppRotateDelay is non-zero, NSSM will pause for the given number of
  247. milliseconds after rotation.
  248. If AppStdoutCopyAndTruncate or AppStderrCopyAndTruncate are non-zero, the
  249. stdout (or stderr respectively) file will be rotated by first taking a copy
  250. of the file then truncating the original file to zero size. This allows
  251. NSSM to rotate files which are held open by other processes, preventing the
  252. usual MoveFile() from succeeding. Note that the copy process may take some
  253. time if the file is large, and will temporarily consume twice as much disk
  254. space as the original file. Note also that applications reading the log file
  255. may not notice that the file size changed. Using this option in conjunction
  256. with AppRotateDelay may help in that case.
  257. Rotation is independent of the CreateFile() parameters used to open the files.
  258. They will be rotated regardless of whether NSSM would otherwise have appended
  259. or replaced them.
  260. NSSM can also rotate files which hit the configured size threshold while the
  261. service is running. Additionally, you can trigger an on-demand rotation by
  262. running the command
  263. nssm rotate <servicename>
  264. On-demand rotations will happen after the next line of data is read from
  265. the managed application, regardless of the value of AppRotateBytes. Be aware
  266. that if the application is not particularly verbose the rotation may not
  267. happen for some time.
  268. To enable online and on-demand rotation, set AppRotateOnline to a non-zero
  269. value.
  270. Note that online rotation requires NSSM to intercept the application's I/O
  271. and create the output files on its behalf. This is more complex and
  272. error-prone than simply redirecting the I/O streams before launching the
  273. application. Therefore online rotation is not enabled by default.
  274. Environment variables
  275. ---------------------
  276. NSSM can replace or append to the managed application's environment. Two
  277. multi-valued string (REG_MULTI_SZ) registry values are recognised under
  278. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters.
  279. AppEnvironment defines a list of environment variables which will override
  280. the service's environment. AppEnvironmentExtra defines a list of
  281. environment variables which will be added to the service's environment.
  282. Each entry in the list should be of the form KEY=VALUE. It is possible to
  283. omit the VALUE but the = symbol is mandatory.
  284. Environment variables listed in both AppEnvironment and AppEnvironmentExtra
  285. are subject to normal expansion, so it is possible, for example, to update the
  286. system path by setting "PATH=C:\bin;%PATH%" in AppEnvironmentExtra. Variables
  287. are expanded in the order in which they appear, so if you want to include the
  288. value of one variable in another variable you should declare the dependency
  289. first.
  290. Because variables defined in AppEnvironment override the existing
  291. environment it is not possible to refer to any variables which were previously
  292. defined.
  293. For example, the following AppEnvironment block:
  294. PATH=C:\Windows\System32;C:\Windows
  295. PATH=C:\bin;%PATH%
  296. Would result in a PATH of "C:\bin;C:\Windows\System32;C:\Windows" as expected.
  297. Whereas the following AppEnvironment block:
  298. PATH=C:\bin;%PATH%
  299. Would result in a path containing only C:\bin and probably cause the
  300. application to fail to start.
  301. Most people will want to use AppEnvironmentExtra exclusively. srvany only
  302. supports AppEnvironment.
  303. As of version 2.25, NSSM parses AppEnvironment and AppEnvironmentExtra
  304. itself, before reading any other registry values. As a result it is now
  305. possible to refer to custom environment variables in Application,
  306. AppDirectory and other parameters.
  307. Merged service environment
  308. --------------------------
  309. All Windows services can be passed additional environment variables by
  310. creating a multi-valued string (REG_MULTI_SZ) registry value named
  311. HLKM\SYSTEM\CurrentControlSet\Services\<service>\Environment.
  312. The contents of this environment block will be merged into the system
  313. environment before the service starts.
  314. Note, however, that the merged environment will be sorted alphabetically
  315. before being processed. This means that in practice you cannot set,
  316. for example, DIR=%PROGRAMFILES% in the Environment block because the
  317. environment passed to the service will not have defined %PROGRAMFILES%
  318. by the time it comes to define %DIR%. Environment variables defined in
  319. AppEnvironmentExtra do not suffer from this limitation.
  320. As of version 2.25, NSSM can get and set the Environment block using
  321. commands similar to:
  322. nssm get <servicename> Environment
  323. It is worth reiterating that the Environment block is available to all
  324. Windows services, not just NSSM services.
  325. Service startup environment
  326. ---------------------------
  327. The environment NSSM passes to the application depends on how various
  328. registry values are configured. The following flow describes how the
  329. environment is modified.
  330. By default:
  331. The service inherits the system environment.
  332. If <service>\Environment is defined:
  333. The contents of Environment are MERGED into the environment.
  334. If <service>\Parameters\AppEnvironment is defined:
  335. The service inherits the environment specified in AppEnvironment.
  336. If <service>\Parameters\AppEnvironmentExtra is defined:
  337. The contents of AppEnvironmentExtra are APPENDED to the environment.
  338. Note that AppEnvironment overrides the system environment and the
  339. merged Environment block. Note also that AppEnvironmentExtra is
  340. guaranteed to be appended to the startup environment if it is defined.
  341. Event hooks
  342. -----------
  343. NSSM can run user-configurable commands in response to application events.
  344. These commands are referred to as "hooks" below.
  345. All hooks are optional. Any hooks which are run will be launched with the
  346. environment configured for the service. NSSM will place additional
  347. variables into the environment which hooks can query to learn how and why
  348. they were called.
  349. Hooks are categorised by Event and Action. Some hooks are run synchronously
  350. and some are run asynchronously. Hooks prefixed with an *asterisk are run
  351. synchronously. NSSM will wait for these hooks to complete before continuing
  352. its work. Note, however, that ALL hooks are subject to a deadline after which
  353. they will be killed, regardless of whether they are run asynchronously
  354. or not.
  355. Event: Start - Triggered when the service is requested to start.
  356. *Action: Pre - Called before NSSM attempts to launch the application.
  357. Action: Post - Called after the application successfully starts.
  358. Event: Stop - Triggered when the service is requested to stop.
  359. *Action: Pre - Called before NSSM attempts to kill the application.
  360. Event: Exit - Triggered when the application exits.
  361. *Action: Post - Called after NSSM has cleaned up the application.
  362. Event: Rotate - Triggered when online log rotation is requested.
  363. *Action: Pre - Called before NSSM rotates logs.
  364. Action: Post - Called after NSSM rotates logs.
  365. Event: Power
  366. Action: Change - Called when the system power status has changed.
  367. Action: Resume - Called when the system has resumed from standby.
  368. Note that there is no Stop/Post hook. This is because Exit/Post is called
  369. when the application exits, regardless of whether it did so in response to
  370. a service shutdown request. Stop/Pre is only called before a graceful
  371. shutdown attempt.
  372. NSSM sets the environment variable NSSM_HOOK_VERSION to a positive number.
  373. Hooks can check the value of the number to determine which other environment
  374. variables are available to them.
  375. If NSSM_HOOK_VERSION is 1 or greater, these variables are provided:
  376. NSSM_EXE - Path to NSSM itself.
  377. NSSM_CONFIGURATION - Build information for the NSSM executable,
  378. eg 64-bit debug.
  379. NSSM_VERSION - Version of the NSSM executable.
  380. NSSM_BUILD_DATE - Build date of NSSM.
  381. NSSM_PID - Process ID of the running NSSM executable.
  382. NSSM_DEADLINE - Deadline number of milliseconds after which NSSM will
  383. kill the hook if it is still running.
  384. NSSM_SERVICE_NAME - Name of the service controlled by NSSM.
  385. NSSM_SERVICE_DISPLAYNAME - Display name of the service.
  386. NSSM_COMMAND_LINE - Command line used to launch the application.
  387. NSSM_APPLICATION_PID - Process ID of the primary application process.
  388. May be blank if the process is not running.
  389. NSSM_EVENT - Event class triggering the hook.
  390. NSSM_ACTION - Event action triggering the hook.
  391. NSSM_TRIGGER - Service control triggering the hook. May be blank if
  392. the hook was not triggered by a service control, eg Exit/Post.
  393. NSSM_LAST_CONTROL - Last service control handled by NSSM.
  394. NSSM_START_REQUESTED_COUNT - Number of times the application was
  395. requested to start.
  396. NSSM_START_COUNT - Number of times the application successfully started.
  397. NSSM_THROTTLE_COUNT - Number of times the application ran for less than
  398. the throttle period. Reset to zero on successful start or when the
  399. service is explicitly unpaused.
  400. NSSM_EXIT_COUNT - Number of times the application exited.
  401. NSSM_EXITCODE - Exit code of the application. May be blank if the
  402. application is still running or has not started yet.
  403. NSSM_RUNTIME - Number of milliseconds for which the NSSM executable has
  404. been running.
  405. NSSM_APPLICATION_RUNTIME - Number of milliseconds for which the
  406. application has been running since it was last started. May be blank
  407. if the application has not been started yet.
  408. Future versions of NSSM may provide more environment variables, in which
  409. case NSSM_HOOK_VERSION will be set to a higher number.
  410. Hooks are configured by creating string (REG_EXPAND_SZ) values in the
  411. registry named after the hook action and placed under
  412. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppEvents\<event>.
  413. For example the service could be configured to restart when the system
  414. resumes from standby by setting AppEvents\Power\Resume to:
  415. %NSSM_EXE% restart %NSSM_SERVICE_NAME%
  416. Note that NSSM will abort the startup of the application if a Start/Pre hook
  417. returns exit code of 99.
  418. A service will normally run hooks in the following order:
  419. Start/Pre
  420. Start/Post
  421. Stop/Pre
  422. Exit/Post
  423. If the application crashes and is restarted by NSSM, the order might be:
  424. Start/Pre
  425. Start/Post
  426. Exit/Post
  427. Start/Pre
  428. Start/Post
  429. Stop/Pre
  430. Exit/Post
  431. If NSSM is redirecting stdout or stderr it can be configured to redirect
  432. the output of any hooks it runs. Set AppRedirectHooks to 1 to enable
  433. that functionality. A hook can of course redirect its own I/O independently
  434. of NSSM.
  435. Managing services using the GUI
  436. -------------------------------
  437. NSSM can edit the settings of existing services with the same GUI that is
  438. used to install them. Run
  439. nssm edit <servicename>
  440. to bring up the GUI.
  441. NSSM offers limited editing capabilities for services other than those which
  442. run NSSM itself. When NSSM is asked to edit a service which does not have
  443. the App* registry settings described above, the GUI will allow editing only
  444. system settings such as the service display name and description.
  445. Managing services using the command line
  446. ----------------------------------------
  447. NSSM can retrieve or set individual service parameters from the command line.
  448. In general the syntax is as follows, though see below for exceptions.
  449. nssm get <servicename> <parameter>
  450. nssm set <servicename> <parameter> <value>
  451. Parameters can also be reset to their default values.
  452. nssm reset <servicename> <parameter>
  453. The parameter names recognised by NSSM are the same as the registry entry
  454. names described above, eg AppDirectory.
  455. NSSM offers limited editing capabilities for Services other than those which
  456. run NSSM itself. The parameters recognised are as follows:
  457. Description: Service description.
  458. DisplayName: Service display name.
  459. Environment: Service merged environment.
  460. ImagePath: Path to the service executable.
  461. ObjectName: User account which runs the service.
  462. Name: Service key name.
  463. Start: Service startup type.
  464. Type: Service type.
  465. These correspond to the registry values under the service's key
  466. HKLM\SYSTEM\CurrentControlSet\Services\<service>.
  467. Note that NSSM will concatenate all arguments passed on the command line
  468. with spaces to form the value to set. Thus the following two invocations
  469. would have the same effect.
  470. nssm set <servicename> Description "NSSM managed service"
  471. nssm set <servicename> Description NSSM managed service
  472. Non-standard parameters
  473. -----------------------
  474. The AppEnvironment, AppEnvironmentExtra and Environment parameters
  475. recognise an additional argument when querying the environment. The
  476. following syntax will print all extra environment variables configured
  477. for a service
  478. nssm get <servicename> AppEnvironmentExtra
  479. whereas the syntax below will print only the value of the CLASSPATH
  480. variable if it is configured in the environment block, or the empty string
  481. if it is not configured.
  482. nssm get <servicename> AppEnvironmentExtra CLASSPATH
  483. When setting an environment block, each variable should be specified as a
  484. KEY=VALUE pair in separate command line arguments. For example:
  485. nssm set <servicename> AppEnvironment CLASSPATH=C:\Classes TEMP=C:\Temp
  486. Alternatively the KEY can be prefixed with a + or - symbol to respectively
  487. add or remove a pair from the block.
  488. The following two lines set CLASSPATH and TEMP:
  489. nssm set <servicename> AppEnvironment CLASSPATH=C:\Classes
  490. nssm set <servicename> AppEnvironment +TEMP=C:\Temp
  491. If the key is already present, specifying +KEY will override the value
  492. while preserving the order of keys:
  493. nssm set <servicename> AppEnvironment +CLASSPATH=C:\NewClasses
  494. The following syntax removes a single variable from the block while
  495. leaving any other variables in place.
  496. nssm set <servicename> AppEnvironment -TEMP
  497. Specifying -KEY=VALUE will remove the variable only if the existing
  498. value matches.
  499. The following syntax would not remove TEMP=C:\Temp
  500. nssm set <servicename> AppEnvironment -TEMP=C:\Work\Temporary
  501. The + and - symbols are valid characters in environment variables.
  502. The syntax :KEY=VALUE is equivalent to KEY=VALUE and can be used to
  503. set variables which start with +/- or to explicitly reset the block in
  504. a script:
  505. nssm set <servicename> AppEnvironment :CLASSPATH=C:\Classes
  506. nssm set <servicename> AppEnvironment +TEMP=C:\Temp
  507. The AppExit parameter requires an additional argument specifying the exit
  508. code to get or set. The default action can be specified with the string
  509. Default.
  510. For example, to get the default exit action for a service you should run
  511. nssm get <servicename> AppExit Default
  512. To get the exit action when the application exits with exit code 2, run
  513. nssm get <servicename> AppExit 2
  514. Note that if no explicit action is configured for a specified exit code,
  515. NSSM will print the default exit action.
  516. To set configure the service to stop when the application exits with an
  517. exit code of 2, run
  518. nssm set <servicename> AppExit 2 Exit
  519. The AppPriority parameter is used to set the priority class of the
  520. managed application. Valid priorities are as follows:
  521. REALTIME_PRIORITY_CLASS
  522. HIGH_PRIORITY_CLASS
  523. ABOVE_NORMAL_PRIORITY_CLASS
  524. NORMAL_PRIORITY_CLASS
  525. BELOW_NORMAL_PRIORITY_CLASS
  526. IDLE_PRIORITY_CLASS
  527. The DependOnGroup and DependOnService parameters are used to query or set
  528. the dependencies for the service. When setting dependencies, each service
  529. or service group (preceded with the + symbol) should be specified in
  530. separate command line arguments. For example:
  531. nssm set <servicename> DependOnService RpcSs LanmanWorkstation
  532. Alternatively the dependency name can be prefixed with a + or - symbol to
  533. respectively add or remove a dependency.
  534. The following two lines set dependencies on RpcSs and LanmanWorkstation:
  535. nssm set <servicename> DependOnService RpcSs
  536. nssm set <servicename> DependOnService +LanmanWorkstation
  537. The follwing syntax removes the dependency on RpcSs:
  538. nssm set <servicename> DependOnService -RpcSs
  539. Service groups should, strictly speaking, be prefixed with the + symbol.
  540. To specify a single dependency on a group, the + symbol can be prefixed
  541. with the : symbol.
  542. The following lines are equivalent, and each set a dependency ONLY on
  543. NetBIOSGroup:
  544. nssm set <servicename> DependOnGroup NetBIOSGroup
  545. nssm set <servicename> DependOnGroup :NetBIOSGroup
  546. nssm set <servicename> DependOnGroup :+NetBIOSGroup
  547. Whereas these lines add to any existing dependencies:
  548. nssm set <servicename> DependOnGroup +NetBIOSGroup
  549. nssm set <servicename> DependOnGroup ++NetBIOSGroup
  550. The Name parameter can only be queried, not set. It returns the service's
  551. registry key name. This may be useful to know if you take advantage of
  552. the fact that you can substitute the service's display name anywhere where
  553. the syntax calls for <servicename>.
  554. The ObjectName parameter requires an additional argument only when setting
  555. a username. The additional argument is the password of the user.
  556. To retrieve the username, run
  557. nssm get <servicename> ObjectName
  558. To set the username and password, run
  559. nssm set <servicename> ObjectName <username> <password>
  560. Note that the rules of argument concatenation still apply. The following
  561. invocation is valid and will have the expected effect.
  562. nssm set <servicename> ObjectName <username> correct horse battery staple
  563. The following well-known usernames do not need a password. The password
  564. parameter can be omitted when using them:
  565. "LocalSystem" aka "System" aka "NT Authority\System"
  566. "LocalService" aka "Local Service" aka "NT Authority\Local Service"
  567. "NetworkService" aka "Network Service" aka "NT Authority\Network Service"
  568. The Start parameter is used to query or set the startup type of the service.
  569. Valid service startup types are as follows:
  570. SERVICE_AUTO_START: Automatic startup at boot.
  571. SERVICE_DELAYED_START: Delayed startup at boot.
  572. SERVICE_DEMAND_START: Manual service startup.
  573. SERVICE_DISABLED: The service is disabled.
  574. Note that SERVICE_DELAYED_START is not supported on versions of Windows prior
  575. to Vista. NSSM will set the service to automatic startup if delayed start is
  576. unavailable.
  577. The Type parameter is used to query or set the service type. NSSM recognises
  578. all currently documented service types but will only allow setting one of two
  579. types:
  580. SERVICE_WIN32_OWN_PROCESS: A standalone service. This is the default.
  581. SERVICE_INTERACTIVE_PROCESS: A service which can interact with the desktop.
  582. Note that a service may only be configured as interactive if it runs under
  583. the LocalSystem account. The safe way to configure an interactive service
  584. is in two stages as follows.
  585. nssm reset <servicename> ObjectName
  586. nssm set <servicename> Type SERVICE_INTERACTIVE_PROCESS
  587. Controlling services using the command line
  588. -------------------------------------------
  589. NSSM offers rudimentary service control features.
  590. nssm start <servicename>
  591. nssm restart <servicename>
  592. nssm stop <servicename>
  593. nssm status <servicename>
  594. Removing services using the GUI
  595. -------------------------------
  596. NSSM can also remove services. Run
  597. nssm remove <servicename>
  598. to remove a service. You will prompted for confirmation before the service
  599. is removed. Try not to remove essential system services...
  600. Removing service using the command line
  601. ---------------------------------------
  602. To remove a service without confirmation from the GUI, run
  603. nssm remove <servicename> confirm
  604. Try not to remove essential system services...
  605. Logging
  606. -------
  607. NSSM logs to the Windows event log. It registers itself as an event log source
  608. and uses unique event IDs for each type of message it logs. New versions may
  609. add event types but existing event IDs will never be changed.
  610. Because of the way NSSM registers itself you should be aware that you may not
  611. be able to replace the NSSM binary if you have the event viewer open and that
  612. running multiple instances of NSSM from different locations may be confusing if
  613. they are not all the same version.
  614. Listing managed services
  615. ------------------------
  616. The following command will print the names of all services managed by NSSM:
  617. nssm list
  618. Showing processes started by a service
  619. --------------------------------------
  620. The following command will print the process ID and executable path of
  621. processes started by a given service:
  622. nssm processes <servicename>
  623. Note that if 32-bit NSSM is run on a 64-bit system running an older version of
  624. Windows than Vista it will not be able to query the paths of 64-bit processes.
  625. Exporting service configuration
  626. -------------------------------
  627. NSSM can dump commands which would recreate the configuration of a service.
  628. The output can be pasted into a batch script to back up the service or
  629. transfer to another computer.
  630. nssm dump <servicename>
  631. Because the service configuration may contain characters which need to be
  632. quoted or escaped from the command prompt, NSSM tries hard to produce
  633. output which will work correctly when run as a script, by adding quotes
  634. and caret escapes as appropriate.
  635. To facilitate copying a service, the dump command accepts a second
  636. argument which specifies the name of the service to be used in the output.
  637. nssm dump <servicename> <newname>
  638. Lines in the dump will reference the <newname> service while showing the
  639. configuration of <servicename>.
  640. Example usage
  641. -------------
  642. To install an Unreal Tournament server:
  643. nssm install UT2004 c:\games\ut2004\system\ucc.exe server
  644. To run the server as the "games" user:
  645. nssm set UT2004 ObjectName games password
  646. To configure the server to log to a file:
  647. nssm set UT2004 AppStdout c:\games\ut2004\service.log
  648. To restrict the server to a single CPU:
  649. nssm set UT2004 AppAffinity 0
  650. To remove the server:
  651. nssm remove UT2004 confirm
  652. To find out the service name of a service with a display name:
  653. nssm get "Background Intelligent Transfer Service" Name
  654. Building NSSM from source
  655. -------------------------
  656. NSSM is known to compile with Visual Studio 2008 and later. Older Visual
  657. Studio releases may or may not work if you install an appropriate SDK and
  658. edit the nssm.vcproj and nssm.sln files to set a lower version number.
  659. They are known not to work with default settings.
  660. NSSM will also compile with Visual Studio 2010 but the resulting executable
  661. will not run on versions of Windows older than XP SP2. If you require
  662. compatiblity with older Windows releases you should change the Platform
  663. Toolset to v90 in the General section of the project's Configuration
  664. Properties.
  665. Credits
  666. -------
  667. Thanks to Bernard Loh for finding a bug with service recovery.
  668. Thanks to Benjamin Mayrargue (www.softlion.com) for adding 64-bit support.
  669. Thanks to Joel Reingold for spotting a command line truncation bug.
  670. Thanks to Arve Knudsen for spotting that child processes of the monitored
  671. application could be left running on service shutdown, and that a missing
  672. registry value for AppDirectory confused NSSM.
  673. Thanks to Peter Wagemans and Laszlo Keresztfalvi for suggesting throttling
  674. restarts.
  675. Thanks to Eugene Lifshitz for finding an edge case in CreateProcess() and for
  676. advising how to build messages.mc correctly in paths containing spaces.
  677. Thanks to Rob Sharp for pointing out that NSSM did not respect the
  678. AppEnvironment registry value used by srvany.
  679. Thanks to Szymon Nowak for help with Windows 2000 compatibility.
  680. Thanks to François-Régis Tardy and Gildas le Nadan for French translation.
  681. Thanks to Emilio Frini for spotting that French was inadvertently set as
  682. the default language when the user's display language was not translated.
  683. Thanks to Riccardo Gusmeroli and Marco Certelli for Italian translation.
  684. Thanks to Eric Cheldelin for the inspiration to generate a Control-C event
  685. on shutdown.
  686. Thanks to Brian Baxter for suggesting how to escape quotes from the command
  687. prompt.
  688. Thanks to Russ Holmann for suggesting that the shutdown timeout be configurable.
  689. Thanks to Paul Spause for spotting a bug with default registry entries.
  690. Thanks to BUGHUNTER for spotting more GUI bugs.
  691. Thanks to Doug Watson for suggesting file rotation.
  692. Thanks to Арслан Сайдуганов for suggesting setting process priority.
  693. Thanks to Robert Middleton for suggestion and draft implementation of process
  694. affinity support.
  695. Thanks to Andrew RedzMax for suggesting an unconditional restart delay.
  696. Thanks to Bryan Senseman for noticing that applications with redirected stdout
  697. and/or stderr which attempt to read from stdin would fail.
  698. Thanks to Czenda Czendov for help with Visual Studio 2013 and Server 2012R2.
  699. Thanks to Alessandro Gherardi for reporting and draft fix of the bug whereby
  700. the second restart of the application would have a corrupted environment.
  701. Thanks to Hadrien Kohl for suggesting to disable the console window's menu.
  702. Thanks to Allen Vailliencourt for noticing bugs with configuring the service to
  703. run under a local user account.
  704. Thanks to Sam Townsend for noticing a regression with TerminateProcess().
  705. Thanks to Barrett Lewis for suggesting the option to skip terminating the
  706. application's child processes.
  707. Thanks to Miguel Angel Terrón for suggesting copy/truncate rotation.
  708. Thanks to Yuriy Lesiuk for suggesting setting the environment before querying
  709. the registry for parameters.
  710. Thanks to Gerald Haider for noticing that installing a service with NSSM in a
  711. path containing spaces was technically a security vulnerability.
  712. Thanks to Scott Ware for reporting a crash saving the environment on XP 32-bit.
  713. Thanks to Stefan and Michael Scherer for reporting a bug writing the event messages source.
  714. Thanks to Paul Baxter for help with Visual Studio 2015.
  715. Thanks to Mathias Breiner for help with Visual Studio and some registry fixes.
  716. Thanks to David Bremner for general tidyups.
  717. Thanks to Nabil Redmann for suggesting redirecting hooks' output.
  718. Thanks to Bader Aldurai for suggesting the process tree.
  719. Licence
  720. -------
  721. NSSM is public domain. You may unconditionally use it and/or its source code
  722. for any purpose you wish.