README.txt 42 KB

  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
  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
  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. Timestamping output
  275. -------------------
  276. When redirecting output, NSSM can prefix each line of output with a
  277. millisecond-precision timestamp, for example:
  278. 2016-09-06 10:17:09.451 Pipeline main started
  279. To enable timestamp prefixing, set AppTimestampLog to a non-zero value.
  280. The prefix applies to both stdout and stderr. Prefixing requires
  281. intercepting the application's I/O in the same way that online rotation
  282. does. If log rotation and timestamp prefixing are both enabled, the
  283. rotation will be online.
  284. Environment variables
  285. ---------------------
  286. NSSM can replace or append to the managed application's environment. Two
  287. multi-valued string (REG_MULTI_SZ) registry values are recognised under
  288. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters.
  289. AppEnvironment defines a list of environment variables which will override
  290. the service's environment. AppEnvironmentExtra defines a list of
  291. environment variables which will be added to the service's environment.
  292. Each entry in the list should be of the form KEY=VALUE. It is possible to
  293. omit the VALUE but the = symbol is mandatory.
  294. Environment variables listed in both AppEnvironment and AppEnvironmentExtra
  295. are subject to normal expansion, so it is possible, for example, to update the
  296. system path by setting "PATH=C:\bin;%PATH%" in AppEnvironmentExtra. Variables
  297. are expanded in the order in which they appear, so if you want to include the
  298. value of one variable in another variable you should declare the dependency
  299. first.
  300. Because variables defined in AppEnvironment override the existing
  301. environment it is not possible to refer to any variables which were previously
  302. defined.
  303. For example, the following AppEnvironment block:
  304. PATH=C:\Windows\System32;C:\Windows
  305. PATH=C:\bin;%PATH%
  306. Would result in a PATH of "C:\bin;C:\Windows\System32;C:\Windows" as expected.
  307. Whereas the following AppEnvironment block:
  308. PATH=C:\bin;%PATH%
  309. Would result in a path containing only C:\bin and probably cause the
  310. application to fail to start.
  311. Most people will want to use AppEnvironmentExtra exclusively. srvany only
  312. supports AppEnvironment.
  313. As of version 2.25, NSSM parses AppEnvironment and AppEnvironmentExtra
  314. itself, before reading any other registry values. As a result it is now
  315. possible to refer to custom environment variables in Application,
  316. AppDirectory and other parameters.
  317. Merged service environment
  318. --------------------------
  319. All Windows services can be passed additional environment variables by
  320. creating a multi-valued string (REG_MULTI_SZ) registry value named
  321. HLKM\SYSTEM\CurrentControlSet\Services\<service>\Environment.
  322. The contents of this environment block will be merged into the system
  323. environment before the service starts.
  324. Note, however, that the merged environment will be sorted alphabetically
  325. before being processed. This means that in practice you cannot set,
  326. for example, DIR=%PROGRAMFILES% in the Environment block because the
  327. environment passed to the service will not have defined %PROGRAMFILES%
  328. by the time it comes to define %DIR%. Environment variables defined in
  329. AppEnvironmentExtra do not suffer from this limitation.
  330. As of version 2.25, NSSM can get and set the Environment block using
  331. commands similar to:
  332. nssm get <servicename> Environment
  333. It is worth reiterating that the Environment block is available to all
  334. Windows services, not just NSSM services.
  335. Service startup environment
  336. ---------------------------
  337. The environment NSSM passes to the application depends on how various
  338. registry values are configured. The following flow describes how the
  339. environment is modified.
  340. By default:
  341. The service inherits the system environment.
  342. If <service>\Environment is defined:
  343. The contents of Environment are MERGED into the environment.
  344. If <service>\Parameters\AppEnvironment is defined:
  345. The service inherits the environment specified in AppEnvironment.
  346. If <service>\Parameters\AppEnvironmentExtra is defined:
  347. The contents of AppEnvironmentExtra are APPENDED to the environment.
  348. Note that AppEnvironment overrides the system environment and the
  349. merged Environment block. Note also that AppEnvironmentExtra is
  350. guaranteed to be appended to the startup environment if it is defined.
  351. Event hooks
  352. -----------
  353. NSSM can run user-configurable commands in response to application events.
  354. These commands are referred to as "hooks" below.
  355. All hooks are optional. Any hooks which are run will be launched with the
  356. environment configured for the service. NSSM will place additional
  357. variables into the environment which hooks can query to learn how and why
  358. they were called.
  359. Hooks are categorised by Event and Action. Some hooks are run synchronously
  360. and some are run asynchronously. Hooks prefixed with an *asterisk are run
  361. synchronously. NSSM will wait for these hooks to complete before continuing
  362. its work. Note, however, that ALL hooks are subject to a deadline after which
  363. they will be killed, regardless of whether they are run asynchronously
  364. or not.
  365. Event: Start - Triggered when the service is requested to start.
  366. *Action: Pre - Called before NSSM attempts to launch the application.
  367. Action: Post - Called after the application successfully starts.
  368. Event: Stop - Triggered when the service is requested to stop.
  369. *Action: Pre - Called before NSSM attempts to kill the application.
  370. Event: Exit - Triggered when the application exits.
  371. *Action: Post - Called after NSSM has cleaned up the application.
  372. Event: Rotate - Triggered when online log rotation is requested.
  373. *Action: Pre - Called before NSSM rotates logs.
  374. Action: Post - Called after NSSM rotates logs.
  375. Event: Power
  376. Action: Change - Called when the system power status has changed.
  377. Action: Resume - Called when the system has resumed from standby.
  378. Note that there is no Stop/Post hook. This is because Exit/Post is called
  379. when the application exits, regardless of whether it did so in response to
  380. a service shutdown request. Stop/Pre is only called before a graceful
  381. shutdown attempt.
  382. NSSM sets the environment variable NSSM_HOOK_VERSION to a positive number.
  383. Hooks can check the value of the number to determine which other environment
  384. variables are available to them.
  385. If NSSM_HOOK_VERSION is 1 or greater, these variables are provided:
  386. NSSM_EXE - Path to NSSM itself.
  387. NSSM_CONFIGURATION - Build information for the NSSM executable,
  388. eg 64-bit debug.
  389. NSSM_VERSION - Version of the NSSM executable.
  390. NSSM_BUILD_DATE - Build date of NSSM.
  391. NSSM_PID - Process ID of the running NSSM executable.
  392. NSSM_DEADLINE - Deadline number of milliseconds after which NSSM will
  393. kill the hook if it is still running.
  394. NSSM_SERVICE_NAME - Name of the service controlled by NSSM.
  395. NSSM_SERVICE_DISPLAYNAME - Display name of the service.
  396. NSSM_COMMAND_LINE - Command line used to launch the application.
  397. NSSM_APPLICATION_PID - Process ID of the primary application process.
  398. May be blank if the process is not running.
  399. NSSM_EVENT - Event class triggering the hook.
  400. NSSM_ACTION - Event action triggering the hook.
  401. NSSM_TRIGGER - Service control triggering the hook. May be blank if
  402. the hook was not triggered by a service control, eg Exit/Post.
  403. NSSM_LAST_CONTROL - Last service control handled by NSSM.
  404. NSSM_START_REQUESTED_COUNT - Number of times the application was
  405. requested to start.
  406. NSSM_START_COUNT - Number of times the application successfully started.
  407. NSSM_THROTTLE_COUNT - Number of times the application ran for less than
  408. the throttle period. Reset to zero on successful start or when the
  409. service is explicitly unpaused.
  410. NSSM_EXIT_COUNT - Number of times the application exited.
  411. NSSM_EXITCODE - Exit code of the application. May be blank if the
  412. application is still running or has not started yet.
  413. NSSM_RUNTIME - Number of milliseconds for which the NSSM executable has
  414. been running.
  415. NSSM_APPLICATION_RUNTIME - Number of milliseconds for which the
  416. application has been running since it was last started. May be blank
  417. if the application has not been started yet.
  418. Future versions of NSSM may provide more environment variables, in which
  419. case NSSM_HOOK_VERSION will be set to a higher number.
  420. Hooks are configured by creating string (REG_EXPAND_SZ) values in the
  421. registry named after the hook action and placed under
  422. HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppEvents\<event>.
  423. For example the service could be configured to restart when the system
  424. resumes from standby by setting AppEvents\Power\Resume to:
  425. %NSSM_EXE% restart %NSSM_SERVICE_NAME%
  426. To set a hook on the command line, use
  427. nssm set <servicename> AppEvents <event>/<action> <command>
  428. Note that NSSM will abort the startup of the application if a Start/Pre hook
  429. returns exit code of 99.
  430. A service will normally run hooks in the following order:
  431. Start/Pre
  432. Start/Post
  433. Stop/Pre
  434. Exit/Post
  435. If the application crashes and is restarted by NSSM, the order might be:
  436. Start/Pre
  437. Start/Post
  438. Exit/Post
  439. Start/Pre
  440. Start/Post
  441. Stop/Pre
  442. Exit/Post
  443. If NSSM is redirecting stdout or stderr it can be configured to redirect
  444. the output of any hooks it runs. Set AppRedirectHooks to 1 to enable
  445. that functionality. A hook can of course redirect its own I/O independently
  446. of NSSM.
  447. Managing services using the GUI
  448. -------------------------------
  449. NSSM can edit the settings of existing services with the same GUI that is
  450. used to install them. Run
  451. nssm edit <servicename>
  452. to bring up the GUI.
  453. NSSM offers limited editing capabilities for services other than those which
  454. run NSSM itself. When NSSM is asked to edit a service which does not have
  455. the App* registry settings described above, the GUI will allow editing only
  456. system settings such as the service display name and description.
  457. Managing services using the command line
  458. ----------------------------------------
  459. NSSM can retrieve or set individual service parameters from the command line.
  460. In general the syntax is as follows, though see below for exceptions.
  461. nssm get <servicename> <parameter>
  462. nssm set <servicename> <parameter> <value>
  463. Parameters can also be reset to their default values.
  464. nssm reset <servicename> <parameter>
  465. The parameter names recognised by NSSM are the same as the registry entry
  466. names described above, eg AppDirectory.
  467. NSSM offers limited editing capabilities for Services other than those which
  468. run NSSM itself. The parameters recognised are as follows:
  469. Description: Service description.
  470. DisplayName: Service display name.
  471. Environment: Service merged environment.
  472. ImagePath: Path to the service executable.
  473. ObjectName: User account which runs the service.
  474. Name: Service key name.
  475. Start: Service startup type.
  476. Type: Service type.
  477. These correspond to the registry values under the service's key
  478. HKLM\SYSTEM\CurrentControlSet\Services\<service>.
  479. Note that NSSM will concatenate all arguments passed on the command line
  480. with spaces to form the value to set. Thus the following two invocations
  481. would have the same effect.
  482. nssm set <servicename> Description "NSSM managed service"
  483. nssm set <servicename> Description NSSM managed service
  484. Non-standard parameters
  485. -----------------------
  486. The AppEnvironment, AppEnvironmentExtra and Environment parameters
  487. recognise an additional argument when querying the environment. The
  488. following syntax will print all extra environment variables configured
  489. for a service
  490. nssm get <servicename> AppEnvironmentExtra
  491. whereas the syntax below will print only the value of the CLASSPATH
  492. variable if it is configured in the environment block, or the empty string
  493. if it is not configured.
  494. nssm get <servicename> AppEnvironmentExtra CLASSPATH
  495. When setting an environment block, each variable should be specified as a
  496. KEY=VALUE pair in separate command line arguments. For example:
  497. nssm set <servicename> AppEnvironment CLASSPATH=C:\Classes TEMP=C:\Temp
  498. Alternatively the KEY can be prefixed with a + or - symbol to respectively
  499. add or remove a pair from the block.
  500. The following two lines set CLASSPATH and TEMP:
  501. nssm set <servicename> AppEnvironment CLASSPATH=C:\Classes
  502. nssm set <servicename> AppEnvironment +TEMP=C:\Temp
  503. If the key is already present, specifying +KEY will override the value
  504. while preserving the order of keys:
  505. nssm set <servicename> AppEnvironment +CLASSPATH=C:\NewClasses
  506. The following syntax removes a single variable from the block while
  507. leaving any other variables in place.
  508. nssm set <servicename> AppEnvironment -TEMP
  509. Specifying -KEY=VALUE will remove the variable only if the existing
  510. value matches.
  511. The following syntax would not remove TEMP=C:\Temp
  512. nssm set <servicename> AppEnvironment -TEMP=C:\Work\Temporary
  513. The + and - symbols are valid characters in environment variables.
  514. The syntax :KEY=VALUE is equivalent to KEY=VALUE and can be used to
  515. set variables which start with +/- or to explicitly reset the block in
  516. a script:
  517. nssm set <servicename> AppEnvironment :CLASSPATH=C:\Classes
  518. nssm set <servicename> AppEnvironment +TEMP=C:\Temp
  519. The AppExit parameter requires an additional argument specifying the exit
  520. code to get or set. The default action can be specified with the string
  521. Default.
  522. For example, to get the default exit action for a service you should run
  523. nssm get <servicename> AppExit Default
  524. To get the exit action when the application exits with exit code 2, run
  525. nssm get <servicename> AppExit 2
  526. Note that if no explicit action is configured for a specified exit code,
  527. NSSM will print the default exit action.
  528. To set configure the service to stop when the application exits with an
  529. exit code of 2, run
  530. nssm set <servicename> AppExit 2 Exit
  531. The AppPriority parameter is used to set the priority class of the
  532. managed application. Valid priorities are as follows:
  539. The DependOnGroup and DependOnService parameters are used to query or set
  540. the dependencies for the service. When setting dependencies, each service
  541. or service group (preceded with the + symbol) should be specified in
  542. separate command line arguments. For example:
  543. nssm set <servicename> DependOnService RpcSs LanmanWorkstation
  544. Alternatively the dependency name can be prefixed with a + or - symbol to
  545. respectively add or remove a dependency.
  546. The following two lines set dependencies on RpcSs and LanmanWorkstation:
  547. nssm set <servicename> DependOnService RpcSs
  548. nssm set <servicename> DependOnService +LanmanWorkstation
  549. The follwing syntax removes the dependency on RpcSs:
  550. nssm set <servicename> DependOnService -RpcSs
  551. Service groups should, strictly speaking, be prefixed with the + symbol.
  552. To specify a single dependency on a group, the + symbol can be prefixed
  553. with the : symbol.
  554. The following lines are equivalent, and each set a dependency ONLY on
  555. NetBIOSGroup:
  556. nssm set <servicename> DependOnGroup NetBIOSGroup
  557. nssm set <servicename> DependOnGroup :NetBIOSGroup
  558. nssm set <servicename> DependOnGroup :+NetBIOSGroup
  559. Whereas these lines add to any existing dependencies:
  560. nssm set <servicename> DependOnGroup +NetBIOSGroup
  561. nssm set <servicename> DependOnGroup ++NetBIOSGroup
  562. The Name parameter can only be queried, not set. It returns the service's
  563. registry key name. This may be useful to know if you take advantage of
  564. the fact that you can substitute the service's display name anywhere where
  565. the syntax calls for <servicename>.
  566. The ObjectName parameter requires an additional argument only when setting
  567. a username. The additional argument is the password of the user.
  568. To retrieve the username, run
  569. nssm get <servicename> ObjectName
  570. To set the username and password, run
  571. nssm set <servicename> ObjectName <username> <password>
  572. Note that the rules of argument concatenation still apply. The following
  573. invocation is valid and will have the expected effect.
  574. nssm set <servicename> ObjectName <username> correct horse battery staple
  575. The following well-known usernames do not need a password. The password
  576. parameter can be omitted when using them:
  577. "LocalSystem" aka "System" aka "NT Authority\System"
  578. "LocalService" aka "Local Service" aka "NT Authority\Local Service"
  579. "NetworkService" aka "Network Service" aka "NT Authority\Network Service"
  580. Virtual service account "NT Service\<servicename>"
  581. The Start parameter is used to query or set the startup type of the service.
  582. Valid service startup types are as follows:
  583. SERVICE_AUTO_START: Automatic startup at boot.
  584. SERVICE_DELAYED_START: Delayed startup at boot.
  585. SERVICE_DEMAND_START: Manual service startup.
  586. SERVICE_DISABLED: The service is disabled.
  587. Note that SERVICE_DELAYED_START is not supported on versions of Windows prior
  588. to Vista. NSSM will set the service to automatic startup if delayed start is
  589. unavailable.
  590. The Type parameter is used to query or set the service type. NSSM recognises
  591. all currently documented service types but will only allow setting one of two
  592. types:
  593. SERVICE_WIN32_OWN_PROCESS: A standalone service. This is the default.
  594. SERVICE_INTERACTIVE_PROCESS: A service which can interact with the desktop.
  595. Note that a service may only be configured as interactive if it runs under
  596. the LocalSystem account. The safe way to configure an interactive service
  597. is in two stages as follows.
  598. nssm reset <servicename> ObjectName
  599. nssm set <servicename> Type SERVICE_INTERACTIVE_PROCESS
  600. Controlling services using the command line
  601. -------------------------------------------
  602. NSSM offers rudimentary service control features.
  603. nssm start <servicename>
  604. nssm restart <servicename>
  605. nssm stop <servicename>
  606. nssm status <servicename>
  607. nssm statuscode <servicename>
  608. The output of "nssm status" and "nssm statuscode" is a string
  609. representing the service state, eg SERVICE_RUNNING.
  610. The exit code of "nssm status" will be 0 if the status was
  611. succesfully retrieved. If the exit code is not zero there was
  612. an error.
  613. The exit code of "nssm statuscode" will be the numeric value
  614. of the service state, eg 4 for SERVICE_RUNNING. Zero is not a
  615. valid service state code. If the exit code is zero there was
  616. an error.
  617. Removing services using the GUI
  618. -------------------------------
  619. NSSM can also remove services. Run
  620. nssm remove <servicename>
  621. to remove a service. You will prompted for confirmation before the service
  622. is removed. Try not to remove essential system services...
  623. Removing service using the command line
  624. ---------------------------------------
  625. To remove a service without confirmation from the GUI, run
  626. nssm remove <servicename> confirm
  627. Try not to remove essential system services...
  628. Logging
  629. -------
  630. NSSM logs to the Windows event log. It registers itself as an event log source
  631. and uses unique event IDs for each type of message it logs. New versions may
  632. add event types but existing event IDs will never be changed.
  633. Because of the way NSSM registers itself you should be aware that you may not
  634. be able to replace the NSSM binary if you have the event viewer open and that
  635. running multiple instances of NSSM from different locations may be confusing if
  636. they are not all the same version.
  637. Listing managed services
  638. ------------------------
  639. The following command will print the names of all services managed by NSSM:
  640. nssm list
  641. To see all services on the system, not just NSSM's, use list all:
  642. nssm list all
  643. Showing processes started by a service
  644. --------------------------------------
  645. The following command will print the process ID and executable path of
  646. processes started by a given service:
  647. nssm processes <servicename>
  648. Note that if 32-bit NSSM is run on a 64-bit system running an older version of
  649. Windows than Vista it will not be able to query the paths of 64-bit processes.
  650. Exporting service configuration
  651. -------------------------------
  652. NSSM can dump commands which would recreate the configuration of a service.
  653. The output can be pasted into a batch script to back up the service or
  654. transfer to another computer.
  655. nssm dump <servicename>
  656. Because the service configuration may contain characters which need to be
  657. quoted or escaped from the command prompt, NSSM tries hard to produce
  658. output which will work correctly when run as a script, by adding quotes
  659. and caret escapes as appropriate.
  660. To facilitate copying a service, the dump command accepts a second
  661. argument which specifies the name of the service to be used in the output.
  662. nssm dump <servicename> <newname>
  663. Lines in the dump will reference the <newname> service while showing the
  664. configuration of <servicename>.
  665. Example usage
  666. -------------
  667. To install an Unreal Tournament server:
  668. nssm install UT2004 c:\games\ut2004\system\ucc.exe server
  669. To run the server as the "games" user:
  670. nssm set UT2004 ObjectName games password
  671. To configure the server to log to a file:
  672. nssm set UT2004 AppStdout c:\games\ut2004\service.log
  673. To restrict the server to a single CPU:
  674. nssm set UT2004 AppAffinity 0
  675. To remove the server:
  676. nssm remove UT2004 confirm
  677. To find out the service name of a service with a display name:
  678. nssm get "Background Intelligent Transfer Service" Name
  679. Building NSSM from source
  680. -------------------------
  681. NSSM is known to compile with Visual Studio 2008 and later. Older Visual
  682. Studio releases may or may not work if you install an appropriate SDK and
  683. edit the nssm.vcproj and nssm.sln files to set a lower version number.
  684. They are known not to work with default settings.
  685. NSSM will also compile with Visual Studio 2010 but the resulting executable
  686. will not run on versions of Windows older than XP SP2. If you require
  687. compatiblity with older Windows releases you should change the Platform
  688. Toolset to v90 in the General section of the project's Configuration
  689. Properties.
  690. Credits
  691. -------
  692. Thanks to Bernard Loh for finding a bug with service recovery.
  693. Thanks to Benjamin Mayrargue ( for adding 64-bit support.
  694. Thanks to Joel Reingold for spotting a command line truncation bug.
  695. Thanks to Arve Knudsen for spotting that child processes of the monitored
  696. application could be left running on service shutdown, and that a missing
  697. registry value for AppDirectory confused NSSM.
  698. Thanks to Peter Wagemans and Laszlo Keresztfalvi for suggesting throttling
  699. restarts.
  700. Thanks to Eugene Lifshitz for finding an edge case in CreateProcess() and for
  701. advising how to build correctly in paths containing spaces.
  702. Thanks to Rob Sharp for pointing out that NSSM did not respect the
  703. AppEnvironment registry value used by srvany.
  704. Thanks to Szymon Nowak for help with Windows 2000 compatibility.
  705. Thanks to François-Régis Tardy and Gildas le Nadan for French translation.
  706. Thanks to Emilio Frini for spotting that French was inadvertently set as
  707. the default language when the user's display language was not translated.
  708. Thanks to Riccardo Gusmeroli and Marco Certelli for Italian translation.
  709. Thanks to Eric Cheldelin for the inspiration to generate a Control-C event
  710. on shutdown.
  711. Thanks to Brian Baxter for suggesting how to escape quotes from the command
  712. prompt.
  713. Thanks to Russ Holmann for suggesting that the shutdown timeout be configurable.
  714. Thanks to Paul Spause for spotting a bug with default registry entries.
  715. Thanks to BUGHUNTER for spotting more GUI bugs.
  716. Thanks to Doug Watson for suggesting file rotation.
  717. Thanks to Арслан Сайдуганов for suggesting setting process priority.
  718. Thanks to Robert Middleton for suggestion and draft implementation of process
  719. affinity support.
  720. Thanks to Andrew RedzMax for suggesting an unconditional restart delay.
  721. Thanks to Bryan Senseman for noticing that applications with redirected stdout
  722. and/or stderr which attempt to read from stdin would fail.
  723. Thanks to Czenda Czendov for help with Visual Studio 2013 and Server 2012R2.
  724. Thanks to Alessandro Gherardi for reporting and draft fix of the bug whereby
  725. the second restart of the application would have a corrupted environment.
  726. Thanks to Hadrien Kohl for suggesting to disable the console window's menu.
  727. Thanks to Allen Vailliencourt for noticing bugs with configuring the service to
  728. run under a local user account.
  729. Thanks to Sam Townsend for noticing a regression with TerminateProcess().
  730. Thanks to Barrett Lewis for suggesting the option to skip terminating the
  731. application's child processes.
  732. Thanks to Miguel Angel Terrón for suggesting copy/truncate rotation.
  733. Thanks to Yuriy Lesiuk for suggesting setting the environment before querying
  734. the registry for parameters.
  735. Thanks to Gerald Haider for noticing that installing a service with NSSM in a
  736. path containing spaces was technically a security vulnerability.
  737. Thanks to Scott Ware for reporting a crash saving the environment on XP 32-bit.
  738. Thanks to Stefan and Michael Scherer for reporting a bug writing the event messages source.
  739. Thanks to Paul Baxter for help with Visual Studio 2015.
  740. Thanks to Mathias Breiner for help with Visual Studio and some registry fixes.
  741. Thanks to David Bremner for general tidyups.
  742. Thanks to Nabil Redmann for suggesting redirecting hooks' output.
  743. Thanks to Bader Aldurai for suggesting the process tree.
  744. Thanks to Christian Long for suggesting virtual accounts.
  745. Thanks to Marcin Lewandowski for spotting a bug appending to large files.
  746. Thanks to Nicolas Ducrocq for suggesting timestamping redirected output.
  747. Thanks to Meang Akira Tanaka for suggestion and initial implementation of
  748. the statuscode command.
  749. Thanks to Kirill Kovalenko for reporting a crash with NANO server.
  750. Thanks to Connor Reynolds for spotting a potential buffer overflow.
  751. Thanks to foi for spotting a hang with 64 cores.
  752. Licence
  753. -------
  754. NSSM is public domain. You may unconditionally use it and/or its source code
  755. for any purpose you wish.