Allow setting application priority.
[nssm.git] / README.txt
1 NSSM: The Non-Sucking Service Manager\r
2 Version 2.21, 2013-11-24\r
3 \r
4 NSSM is a service helper program similar to srvany and cygrunsrv.  It can \r
5 start any application as an NT service and will restart the service if it \r
6 fails for any reason.\r
7 \r
8 NSSM also has a graphical service installer and remover.\r
9 \r
10 Full documentation can be found online at\r
11 \r
12                               http://nssm.cc/\r
13 \r
14 Since version 2.0, the GUI can be bypassed by entering all appropriate \r
15 options on the command line.\r
16 \r
17 Since version 2.1, NSSM can be compiled for x64 platforms.\r
18 Thanks Benjamin Mayrargue.\r
19 \r
20 Since version 2.2, NSSM can be configured to take different actions\r
21 based on the exit code of the managed application.\r
22 \r
23 Since version 2.3, NSSM logs to the Windows event log more elegantly.\r
24 \r
25 Since version 2.5, NSSM respects environment variables in its parameters.\r
26 \r
27 Since version 2.8, NSSM tries harder to shut down the managed application\r
28 gracefully and throttles restart attempts if the application doesn't run\r
29 for a minimum amount of time.\r
30 \r
31 Since version 2.11, NSSM respects srvany's AppEnvironment parameter.\r
32 \r
33 Since version 2.13, NSSM is translated into French.\r
34 Thanks François-Régis Tardy.\r
35 \r
36 Since version 2.15, NSSM is translated into Italian.\r
37 Thanks Riccardo Gusmeroli.\r
38 \r
39 Since version 2.17, NSSM can try to shut down console applications by\r
40 simulating a Control-C keypress.  If they have installed a handler routine\r
41 they can clean up and shut down gracefully on receipt of the event.\r
42 \r
43 Since version 2.17, NSSM can redirect the managed application's I/O streams\r
44 to an arbitrary path.\r
45 \r
46 Since version 2.18, NSSM can be configured to wait a user-specified amount\r
47 of time for the application to exit when shutting down.\r
48 \r
49 Since version 2.19, many more service options can be configured with the\r
50 GUI installer as well as via the registry.\r
51 \r
52 Since version 2.19, NSSM can add to the service's environment by setting\r
53 AppEnvironmentExtra in place of or in addition to the srvany-compatible\r
54 AppEnvironment.\r
55 \r
56 Since version 2.22, NSSM can set the managed application's process priority.\r
57 \r
58 Since version 2.22, NSSM can rotate existing output files when redirecting I/O.\r
59 \r
60 Since version 2.22, NSSM can set service display name, description, startup\r
61 type and log on details.\r
62 \r
63 Since version 2.22, NSSM can manage existing services.\r
64 \r
65 \r
66 Usage\r
67 -----\r
68 In the usage notes below, arguments to the program may be written in angle\r
69 brackets and/or square brackets.  <string> means you must insert the\r
70 appropriate string and [<string>] means the string is optional.  See the\r
71 examples below...\r
72 \r
73 Note that everywhere <servicename> appears you may substitute the\r
74 service's display name.\r
75 \r
76 \r
77 Installation using the GUI\r
78 --------------------------\r
79 To install a service, run\r
80 \r
81     nssm install <servicename>\r
82 \r
83 You will be prompted to enter the full path to the application you wish \r
84 to run and any command line options to pass to that application.\r
85 \r
86 Use the system service manager (services.msc) to control advanced service \r
87 properties such as startup method and desktop interaction.  NSSM may \r
88 support these options at a later time...\r
89 \r
90 \r
91 Installation using the command line\r
92 -----------------------------------\r
93 To install a service, run\r
94 \r
95     nssm install <servicename> <application> [<options>]\r
96 \r
97 NSSM will then attempt to install a service which runs the named application \r
98 with the given options (if you specified any).\r
99 \r
100 Don't forget to enclose paths in "quotes" if they contain spaces!\r
101 \r
102 If you want to include quotes in the options you will need to """quote""" the\r
103 quotes.\r
104 \r
105 \r
106 Managing the service\r
107 --------------------\r
108 NSSM will launch the application listed in the registry when you send it a \r
109 start signal and will terminate it when you send a stop signal.  So far, so \r
110 much like srvany.  But NSSM is the Non-Sucking service manager and can take \r
111 action if/when the application dies.\r
112 \r
113 With no configuration from you, NSSM will try to restart itself if it notices\r
114 that the application died but you didn't send it a stop signal.  NSSM will\r
115 keep trying, pausing between each attempt, until the service is successfully\r
116 started or you send it a stop signal.\r
117 \r
118 NSSM will pause an increasingly longer time between subsequent restart attempts\r
119 if the service fails to start in a timely manner, up to a maximum of four\r
120 minutes.  This is so it does not consume an excessive amount of CPU time trying\r
121 to start a failed application over and over again.  If you identify the cause\r
122 of the failure and don't want to wait you can use the Windows service console\r
123 (where the service will be shown in Paused state) to send a continue signal to\r
124 NSSM and it will retry within a few seconds.\r
125 \r
126 By default, NSSM defines "a timely manner" to be within 1500 milliseconds.\r
127 You can change the threshold for the service by setting the number of\r
128 milliseconds as a REG_DWORD value in the registry at\r
129 HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppThrottle.\r
130 \r
131 NSSM will look in the registry under\r
132 HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppExit for\r
133 string (REG_EXPAND_SZ) values corresponding to the exit code of the application.\r
134 If the application exited with code 1, for instance, NSSM will look for a\r
135 string value under AppExit called "1" or, if it does not find it, will\r
136 fall back to the AppExit (Default) value.  You can find out the exit code\r
137 for the application by consulting the system event log.  NSSM will log the\r
138 exit code when the application exits.\r
139 \r
140 Based on the data found in the registry, NSSM will take one of three actions:\r
141 \r
142 If the value data is "Restart" NSSM will try to restart the application as\r
143 described above.  This is its default behaviour.\r
144 \r
145 If the value data is "Ignore" NSSM will not try to restart the application\r
146 but will continue running itself.  This emulates the (usually undesirable)\r
147 behaviour of srvany.  The Windows Services console would show the service\r
148 as still running even though the application has exited.\r
149 \r
150 If the value data is "Exit" NSSM will exit gracefully.  The Windows Services\r
151 console would show the service as stopped.  If you wish to provide\r
152 finer-grained control over service recovery you should use this code and\r
153 edit the failure action manually.  Please note that Windows versions prior\r
154 to Vista will not consider such an exit to be a failure.  On older versions\r
155 of Windows you should use "Suicide" instead.\r
156 \r
157 If the value data is "Suicide" NSSM will simulate a crash and exit without\r
158 informing the service manager.  This option should only be used for\r
159 pre-Vista systems where you wish to apply a service recovery action.  Note\r
160 that if the monitored application exits with code 0, NSSM will only honour a\r
161 request to suicide if you explicitly configure a registry key for exit code 0.\r
162 If only the default action is set to Suicide NSSM will instead exit gracefully.\r
163 \r
164 \r
165 Application priority\r
166 --------------------\r
167 NSSM can set the priority class of the managed application.  NSSM will look in\r
168 the registry under HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\r
169 for the REG_DWORD entry AppPriority.  Valid values correspond to arguments to\r
170 SetProcessPriorityClass().  If AppPriority() is missing or invalid the\r
171 application will be launched with normal priority.\r
172 \r
173 \r
174 Stopping the service\r
175 --------------------\r
176 When stopping a service NSSM will attempt several different methods of killing\r
177 the monitored application, each of which can be disabled if necessary.\r
178 \r
179 First NSSM will attempt to generate a Control-C event and send it to the\r
180 application's console.  Batch scripts or console applications may intercept\r
181 the event and shut themselves down gracefully.  GUI applications do not have\r
182 consoles and will not respond to this method.\r
183 \r
184 Secondly NSSM will enumerate all windows created by the application and send\r
185 them a WM_CLOSE message, requesting a graceful exit.\r
186 \r
187 Thirdly NSSM will enumerate all threads created by the application and send\r
188 them a WM_QUIT message, requesting a graceful exit.  Not all applications'\r
189 threads have message queues; those which do not will not respond to this\r
190 method.\r
191 \r
192 Finally NSSM will call TerminateProcess() to request that the operating\r
193 system forcibly terminate the application.  TerminateProcess() cannot be\r
194 trapped or ignored, so in most circumstances the application will be killed.\r
195 However, there is no guarantee that it will have a chance to perform any\r
196 tidyup operations before it exits.\r
197 \r
198 Any or all of the methods above may be disabled.  NSSM will look for the\r
199 HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters\AppStopMethodSkip\r
200 registry value which should be of type REG_DWORD set to a bit field describing\r
201 which methods should not be applied.\r
202 \r
203   If AppStopMethodSkip includes 1, Control-C events will not be generated.\r
204   If AppStopMethodSkip includes 2, WM_CLOSE messages will not be posted.\r
205   If AppStopMethodSkip includes 4, WM_QUIT messages will not be posted.\r
206   If AppStopMethodSkip includes 8, TerminateProcess() will not be called.\r
207 \r
208 If, for example, you knew that an application did not respond to Control-C\r
209 events and did not have a thread message queue, you could set AppStopMethodSkip\r
210 to 5 and NSSM would not attempt to use those methods to stop the application.\r
211 \r
212 Take great care when including 8 in the value of AppStopMethodSkip.  If NSSM\r
213 does not call TerminateProcess() it is possible that the application will not\r
214 exit when the service stops.\r
215 \r
216 By default NSSM will allow processes 1500ms to respond to each of the methods\r
217 described above before proceeding to the next one.  The timeout can be\r
218 configured on a per-method basis by creating REG_DWORD entries in the\r
219 registry under HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters.\r
220 \r
221   AppStopMethodConsole\r
222   AppStopMethodWindow\r
223   AppStopMethodThreads\r
224 \r
225 Each value should be set to the number of milliseconds to wait.  Please note\r
226 that the timeout applies to each process in the application's process tree,\r
227 so the actual time to shutdown may be longer than the sum of all configured\r
228 timeouts if the application spawns multiple subprocesses.\r
229 \r
230 \r
231 I/O redirection\r
232 ---------------\r
233 NSSM can redirect the managed application's I/O to any path capable of being\r
234 opened by CreateFile().  This enables, for example, capturing the log output\r
235 of an application which would otherwise only write to the console or accepting\r
236 input from a serial port.\r
237 \r
238 NSSM will look in the registry under\r
239 HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters for the keys\r
240 corresponding to arguments to CreateFile().  All are optional.  If no path is\r
241 given for a particular stream it will not be redirected.  If a path is given\r
242 but any of the other values are omitted they will be receive sensible defaults.\r
243 \r
244   AppStdin: Path to receive input.\r
245   AppStdout: Path to receive output.\r
246   AppStderr: Path to receive error output.\r
247 \r
248 Parameters for CreateFile() are providing with the "AppStdinShareMode",\r
249 "AppStdinCreationDisposition" and "AppStdinFlagsAndAttributes" values (and\r
250 analogously for stdout and stderr).\r
251 \r
252 In general, if you want the service to log its output, set AppStdout and\r
253 AppStderr to the same path, eg C:\Users\Public\service.log, and it should\r
254 work.  Remember, however, that the path must be accessible to the user\r
255 running the service.\r
256 \r
257 \r
258 File rotation\r
259 -------------\r
260 When using I/O redirection, NSSM can rotate existing output files prior to\r
261 opening stdout and/or stderr.  An existing file will be renamed with a\r
262 suffix based on the file's last write time, to millisecond precision.  For\r
263 example, the file nssm.log might be rotated to nssm-20131221T113939.457.log.\r
264 \r
265 NSSM will look in the registry under\r
266 HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters for REG_DWORD\r
267 entries which control how rotation happens.\r
268 \r
269 If AppRotateFiles is missing or set to 0, rotation is disabled.  Any non-zero\r
270 value enables rotation.\r
271 \r
272 If AppRotateSeconds is non-zero, a file will not be rotated if its last write\r
273 time is less than the given number of seconds in the past.\r
274 \r
275 If AppRotateBytes is non-zero, a file will not be rotated if it is smaller\r
276 than the given number of bytes.  64-bit file sizes can be handled by setting\r
277 a non-zero value of AppRotateBytesHigh.\r
278 \r
279 Rotation is independent of the CreateFile() parameters used to open the files.\r
280 They will be rotated regardless of whether NSSM would otherwise have appended\r
281 or replaced them.\r
282 \r
283 \r
284 Environment variables\r
285 ---------------------\r
286 NSSM can replace or append to the managed application's environment.  Two\r
287 multi-valued string (REG_MULTI_SZ) registry values are recognised under\r
288 HKLM\SYSTEM\CurrentControlSet\Services\<service>\Parameters.\r
289 \r
290 AppEnvironment defines a list of environment variables which will override\r
291 the service's environment.  AppEnvironmentExtra defines a list of\r
292 environment variables which will be added to the service's environment.\r
293 \r
294 Each entry in the list should be of the form KEY=VALUE.  It is possible to\r
295 omit the VALUE but the = symbol is mandatory.\r
296 \r
297 srvany only supports AppEnvironment.\r
298 \r
299 \r
300 Managing services using the GUI\r
301 -------------------------------\r
302 NSSM can edit the settings of existing services with the same GUI that is\r
303 used to install them.  Run\r
304 \r
305     nssm edit <servicename>\r
306 \r
307 to bring up the GUI.\r
308 \r
309 NSSM offers limited editing capabilities for services other than those which\r
310 run NSSM itself.  When NSSM is asked to edit a service which does not have\r
311 the App* registry settings described above, the GUI will allow editing only\r
312 system settings such as the service display name and description.\r
313 \r
314 \r
315 Managing services using the command line\r
316 ----------------------------------------\r
317 NSSM can retrieve or set individual service parameters from the command line.\r
318 In general the syntax is as follows, though see below for exceptions.\r
319 \r
320     nssm get <servicename> <parameter>\r
321 \r
322     nssm set <servicename> <parameter> <value>\r
323 \r
324 Parameters can also be reset to their default values.\r
325 \r
326     nssm reset <servicename> <parameter>\r
327 \r
328 The parameter names recognised by NSSM are the same as the registry entry\r
329 names described above, eg AppDirectory.\r
330 \r
331 NSSM offers limited editing capabilities for Services other than those which\r
332 run NSSM itself.  The parameters recognised are as follows:\r
333 \r
334   Description: Service description.\r
335   DisplayName: Service display name.\r
336   ImagePath: Path to the service executable.\r
337   ObjectName: User account which runs the service.\r
338   Name: Service key name.\r
339   Start: Service startup type.\r
340   Type: Service type.\r
341 \r
342 These correspond to the registry values under the service's key\r
343 HKLM\SYSTEM\CurrentControlSet\Services\<service>.\r
344 \r
345 \r
346 Note that NSSM will concatenate all arguments passed on the command line\r
347 with spaces to form the value to set.  Thus the following two invocations\r
348 would have the same effect.\r
349 \r
350     nssm set <servicename> Description "NSSM managed service"\r
351 \r
352     nssm set <servicename> Description NSSM managed service\r
353 \r
354 \r
355 Non-standard parameters\r
356 -----------------------\r
357 The AppEnvironment and AppEnvironmentExtra parameters recognise an\r
358 additional argument when querying the environment.  The following syntax\r
359 will print all extra environment variables configured for a service\r
360 \r
361     nssm get <servicename> AppEnvironmentExtra\r
362 \r
363 whereas the syntax below will print only the value of the CLASSPATH\r
364 variable if it is configured in the environment block, or the empty string\r
365 if it is not configured.\r
366 \r
367     nssm get <servicename> AppEnvironmentExtra CLASSPATH\r
368 \r
369 When setting an environment block, each variable should be specified as a\r
370 KEY=VALUE pair in separate command line arguments.  For example:\r
371 \r
372     nssm set <servicename> AppEnvironment CLASSPATH=C:\Classes TEMP=C:\Temp\r
373 \r
374 \r
375 The AppExit parameter requires an additional argument specifying the exit\r
376 code to get or set.  The default action can be specified with the string\r
377 Default.\r
378 \r
379 For example, to get the default exit action for a service you should run\r
380 \r
381     nssm get <servicename> AppExit Default\r
382 \r
383 To get the exit action when the application exits with exit code 2, run\r
384 \r
385     nssm get <servicename> AppExit 2\r
386 \r
387 Note that if no explicit action is configured for a specified exit code,\r
388 NSSM will print the default exit action.\r
389 \r
390 To set configure the service to stop when the application exits with an\r
391 exit code of 2, run\r
392 \r
393     nssm set <servicename> AppExit 2 Exit\r
394 \r
395 \r
396 The AppPriority parameter is used to set the priority class of the\r
397 managed application.  Valid priorities are as follows:\r
398 \r
399   REALTIME_PRIORITY_CLASS\r
400   HIGH_PRIORITY_CLASS\r
401   ABOVE_NORMAL_PRIORITY_CLASS\r
402   NORMAL_PRIORITY_CLASS\r
403   BELOW_NORMAL_PRIORITY_CLASS\r
404   IDLE_PRIORITY_CLASS\r
405 \r
406 \r
407 The Name parameter can only be queried, not set.  It returns the service's\r
408 registry key name.  This may be useful to know if you take advantage of\r
409 the fact that you can substitute the service's display name anywhere where\r
410 the syntax calls for <servicename>.\r
411 \r
412 \r
413 The ObjectName parameter requires an additional argument only when setting\r
414 a username.  The additional argument is the password of the user.\r
415 \r
416 To retrieve the username, run\r
417 \r
418     nssm get <servicename> ObjectName\r
419 \r
420 To set the username and password, run\r
421 \r
422     nssm set <servicename> ObjectName <username> <password>\r
423 \r
424 Note that the rules of argument concatenation still apply.  The following\r
425 invocation is valid and will have the expected effect.\r
426 \r
427     nssm set <servicename> ObjectName <username> correct horse battery staple\r
428 \r
429 \r
430 The Start parameter is used to query or set the startup type of the service.\r
431 Valid service startup types are as follows:\r
432 \r
433   SERVICE_AUTO_START: Automatic startup at boot.\r
434   SERVICE_DELAYED_START: Delayed startup at boot.\r
435   SERVICE_DEMAND_START: Manual service startup.\r
436   SERVICE_DISABLED: The service is disabled.\r
437 \r
438 Note that SERVICE_DELAYED_START is not supported on versions of Windows prior\r
439 to Vista.  NSSM will set the service to automatic startup if delayed start is\r
440 unavailable.\r
441 \r
442 \r
443 The Type parameter is used to query or set the service type.  NSSM recognises\r
444 all currently documented service types but will only allow setting one of two\r
445 types:\r
446 \r
447   SERVICE_WIN32_OWN_PROCESS: A standalone service.  This is the default.\r
448   SERVICE_INTERACTIVE_PROCESS: A service which can interact with the desktop.\r
449 \r
450 Note that a service may only be configured as interactive if it runs under\r
451 the LocalSystem account.  The safe way to configure an interactive service\r
452 is in two stages as follows.\r
453 \r
454     nssm reset <servicename> ObjectName\r
455     nssm set <servicename> Type SERVICE_INTERACTIVE_PROCESS\r
456 \r
457 \r
458 Controlling services using the command line\r
459 -------------------------------------------\r
460 NSSM offers rudimentary service control features.\r
461 \r
462     nssm start <servicename>\r
463 \r
464     nssm stop <servicename>\r
465 \r
466     nssm status <servicename>\r
467 \r
468 \r
469 Removing services using the GUI\r
470 -------------------------------\r
471 NSSM can also remove services.  Run\r
472 \r
473     nssm remove <servicename>\r
474 \r
475 to remove a service.  You will prompted for confirmation before the service \r
476 is removed.  Try not to remove essential system services...\r
477 \r
478 \r
479 Removing service using the command line\r
480 ---------------------------------------\r
481 To remove a service without confirmation from the GUI, run\r
482 \r
483     nssm remove <servicename> confirm\r
484 \r
485 Try not to remove essential system services...\r
486 \r
487 \r
488 Logging\r
489 -------\r
490 NSSM logs to the Windows event log.  It registers itself as an event log source\r
491 and uses unique event IDs for each type of message it logs.  New versions may\r
492 add event types but existing event IDs will never be changed.\r
493 \r
494 Because of the way NSSM registers itself you should be aware that you may not\r
495 be able to replace the NSSM binary if you have the event viewer open and that\r
496 running multiple instances of NSSM from different locations may be confusing if\r
497 they are not all the same version.\r
498 \r
499 \r
500 Example usage\r
501 -------------\r
502 To install an Unreal Tournament server:\r
503 \r
504     nssm install UT2004 c:\games\ut2004\system\ucc.exe server\r
505 \r
506 To run the server as the "games" user:\r
507 \r
508     nssm set UT2004 ObjectName games password\r
509 \r
510 To configure the server to log to a file:\r
511 \r
512     nssm set UT2004 AppStdout c:\games\ut2004\service.log\r
513 \r
514 To remove the server:\r
515 \r
516     nssm remove UT2004 confirm\r
517 \r
518 To find out the service name of a service with a display name:\r
519 \r
520     nssm get "Background Intelligent Transfer Service" Name\r
521 \r
522 \r
523 Building NSSM from source\r
524 -------------------------\r
525 NSSM is known to compile with Visual Studio 2008 and later.  Older Visual\r
526 Studio releases may or may not work if you install an appropriate SDK and\r
527 edit the nssm.vcproj and nssm.sln files to set a lower version number.\r
528 They are known not to work with default settings.\r
529 \r
530 NSSM will also compile with Visual Studio 2010 but the resulting executable\r
531 will not run on versions of Windows older than XP SP2.  If you require\r
532 compatiblity with older Windows releases you should change the Platform\r
533 Toolset to v90 in the General section of the project's Configuration\r
534 Properties.\r
535 \r
536 \r
537 Credits\r
538 -------\r
539 Thanks to Bernard Loh for finding a bug with service recovery.\r
540 Thanks to Benjamin Mayrargue (www.softlion.com) for adding 64-bit support.\r
541 Thanks to Joel Reingold for spotting a command line truncation bug.\r
542 Thanks to Arve Knudsen for spotting that child processes of the monitored\r
543 application could be left running on service shutdown, and that a missing\r
544 registry value for AppDirectory confused NSSM.\r
545 Thanks to Peter Wagemans and Laszlo Keresztfalvi for suggesting throttling restarts.\r
546 Thanks to Eugene Lifshitz for finding an edge case in CreateProcess() and for\r
547 advising how to build messages.mc correctly in paths containing spaces.\r
548 Thanks to Rob Sharp for pointing out that NSSM did not respect the\r
549 AppEnvironment registry value used by srvany.\r
550 Thanks to Szymon Nowak for help with Windows 2000 compatibility.\r
551 Thanks to François-Régis Tardy for French translation.\r
552 Thanks to Emilio Frini for spotting that French was inadvertently set as\r
553 the default language when the user's display language was not translated.\r
554 Thanks to Riccardo Gusmeroli for Italian translation.\r
555 Thanks to Eric Cheldelin for the inspiration to generate a Control-C event\r
556 on shutdown.\r
557 Thanks to Brian Baxter for suggesting how to escape quotes from the command prompt.\r
558 Thanks to Russ Holmann for suggesting that the shutdown timeout be configurable.\r
559 Thanks to Paul Spause for spotting a bug with default registry entries.\r
560 Thanks to BUGHUNTER for spotting more GUI bugs.\r
561 Thanks to Doug Watson for suggesting file rotation.\r
562 Thanks to Арслан Сайдуганов for suggesting setting process priority.\r
563 \r
564 Licence\r
565 -------\r
566 NSSM is public domain.  You may unconditionally use it and/or its source code \r
567 for any purpose you wish.\r