NSSM - the Non-Sucking Service Manager
No "installation" of nssm is needed. Just place it somewhere on the system (preferably somewhere in your PATH) and run it.
Note however that nssm registers itself as an Event Log message source which means that running multiple instances or different version of nssm from different locations may cause confusion. Also note that if you run the Event Viewer it will open the nssm executable, preventing you from overwriting it. Keep this in mind if you come to upgrade nssm.
Installing a service
You can use nssm to install a service. The command to type is:
nssm install <servicename>
The installer consists of several tabs with lots of configurable parameters. Most are preset to nssm's defaults, so it's possible to install a service without leaving the Application tab.
The Path to the application (or script) you want to run and is the only mandatory field. If the application needs to start in a particular directory you can enter it in the Startup directory field. If the field is left blank the default startup directory will be the directory containing the application. The Options field can be used to specify any commandline options to pass to the application.
The screenshot below shows installation of a UT2003 server. The command to run such a service is ucc server so the full path to UCC.exe is entered under Path and server is entered under Options.
Clicking Install service completes the installation of the service.
The Shutdown tab lists the various stop methods and timeouts used when tidying up the application after a crash or when the service is gracefully stopped.
Exit actions tab
To configure exit actions for specific application exit codes you must use the registry as described below.
The I/O tab can be used to specify the input and/or output files used when I/O redirection is enabled. Setting Output and Error is usually sufficient to capture log messages generated by the application. If the Replace files checkbox is checked, nssm will overwrite existing output files. The default is to append to any existing files. Note that if you choose to overwrite output files you run the risk of losing previous output when the service is restarted It is recommended that you use an exit action other than Restart (the default) in conjunction with Replace files.
Configure I/O in the registry as described below for more control over paths and access modes.
The Environment tab can be used to specify a newline-separated list of environment variables to pass to the application. If the Replace default environment checkbox is checked the variables specified will be the only ones passed to the service. When it is left unchecked (the default), the environment created at service startup will be preserved.
Installing from the command line
As of version 2.0 you can also bypass the GUI and install a service from the command line. The syntax is:
nssm install <servicename> <application> [<options>]
Please note that the actual program entered into the services database is nssm itself so you must not move or delete nssm.exe after installing a service. If you do wish to change the path to nssm.exe you can either remove and reinstall the service or edit HKLM\System\CurrentControlSet\Services\servicename\ImagePath to reflect the new location.
nssm correctly handles paths with spaces but passing arguments to it can be tricky because of how the command prompt works.
If the path to the application contains spaces you will need to enclose it in quotes otherwise the command prompt will interpret the path as two arguments.
nssm install <servicename> "C:\Program Files\app.exe"
If one of the options you wish to provide contains spaces, you will need to quote that too and quote the quotation marks themselves.
nssm install <servicename> <application> """This is one argument"""
Isaballa Sanfelipo suggests a method of installing a Java application from a batch file.
nssm install solr "%JavaExe%" -Dsolr.solr.home="\"%CD%\solr"\" -Djetty.home="\"%CD%"\" -Djetty.logs="\"%CD%\logs"\" -cp "\"%CD%\lib\*.jar"\";"\"%CD%\start.jar"\" -jar "\"%CD%\start.jar"\"
Removing a service
The command to remove a service is:
nssm remove <servicename>
A confirmation window is displayed before the service is removed.
As of version 2.0 you can also remove services from the command line viz:
nssm remove <servicename> confirm
nssm will happily try to remove any service, not just ones nssm itself manages. Try not to delete services you shouldn't...
When nssm receives a stop command from the Windows service manager, or when it detects that the monitored application has exited, it tries to shut down the monitored application, and any subprocesses, gracefully. If the application's process tree does not exit promptly, nssm can forcibly terminate all processes and subprocesses belonging to the application.
There are four stages which nssm can use to shut down the application, and by default it will attempt all four in order. It is possible (though not recommended) to disable some or all of the methods from being used. Different applications will respond differently to the various requests, so leaving them all enabled is usually the best way to ensure that the application shuts down gracefully.
First nssm will attempt to generate a Control-C event and send it to the application's console. Batch scripts or console applications may intercept the event and shut themselves down gracefully. Java applications tend to respond well to Control-C events. GUI applications do not have consoles and will not respond to this method. Not supported on Windows 2000.
Secondly nssm will enumerate all windows created by the application and send them a WM_CLOSE message. Applications may follow the convention of responding to the message by initiating a graceful exit.
Thirdly nssm will enumerate all threads created by the application and send them a WM_QUIT message, which will be received if the application has a thread message queue.
As a last resort nssm can call
request that the operating system forcibly terminate the application.
TerminateProcess() call cannot be trapped or ignored, so in
most circumstances the application will be killed. However, it is unlikely
that it will be able to perform any cleanup operations before it exits.
To disable any of the methods above, create an integer (REG_DWORD) value HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppStopMethodSkip and set it to the sum of one or more of the numbers below.
1 - Don't send Control-C to the console.
2 - Don't send WM_CLOSE to windows.
4 - Don't send WM_QUIT to threads.
8 - Don't call TerminateProcess().
If, for example, you knew that an application did not respond to Control-C and did not have a thread message queue, you could set AppStopMethodSkip to 5.
It is highly recommended not to disable the
TerminateProcess() call. When the service is stopped
nssm will exit. If the application is not terminated before that
happens it may continue running and nssm will no longer be able to
By default nssm will wait up to 1500 milliseconds for the application to exit after trying each of the methods described above. The timeout can be configured on a per-method basis by creating integer (REG_DWORD) values under HKLM\System\CurrentControlSet\Services\servicename\Parameters in the registry and setting them to the desired number of milliseconds to wait.
AppStopMethodConsole - Time to wait after sending Control-C.
AppStopMethodWindow - Time to wait after sending WM_CLOSE.
AppStopMethodThreads - Time to wait after sending WM_QUIT.
Note that the timeout applies to all processes spawned by the application so the total timeout may be longer than expected if the application has multiple subprocesses.
Actions on exit
To configure the action which nssm should take when the application exits, edit the default value of the key HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppExit. If the key does not exist in the registry when nssm runs it will create it and set the value to Restart. Change it to either Ignore or Exit to specify the action taken. nssm will only create this key if it doesn't already exist. Your changes will not be overridden.
To specify a different action for particular exit codes, create a string (REG_SZ) value underneath the AppExit key whose name is the exit code being considered. For example to stop the service on an exit code of 0 (which usually means the application finished successfully), create HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppExit\0 and set it to Exit. Look in the Event Log for messages from nssm to see what exit codes are returned by your application.
If your application's exit code does not correspond to a registry entry, nssm will use the default value of AppExit when deciding what to do.
To avoid a tight CPU loop, nssm will throttle restarts of the service if the monitored application exits too soon after starting. By default a threshold of 1500 milliseconds is used. To specify a different value, create an integer (REG_DWORD) value HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppThrottle and set it to the required number of milliseconds.
The first restart will be attempted with no delay. If the restarted application continues to exit before running for the threshold amount of milliseconds, nssm will pause for at least 2000 milliseconds, doubling the pause time for each subsequent failure. The maximum time it will pause is 256000 milliseconds, around four minutes. The delay counter is reset when the service successfully runs for at least the threshold time.
If you determine why the service failed and take action to correct the problem, you can use the Windows service manager to send a continue signal to the service, which will be shown as Paused. In this way you can avoid having to wait for the next restart attempt.
nssm can redirect the managed application's I/O to any path
capable of being opened by
CreateFile(). This feature may be
useful if your application expects to be able to log to the console.
nssm will look under
for keys corresponding to arguments to
CreateFile(). All are
optional. If no path is given for a particular stream it will not be
redirected. If a path is given but any of the other values they will receive
AppStdin (string) - Path to receive input.
AppStdinShareMode (integer) -
ShareModeargument for the input.
AppStdinCreationDisposition (integer) -
CreationDispositionargument for the input.
AppStdinFlagsAndAttributes (integer) -
FlagsAndAttributesargument for the input.
AppStdout (string) - Path to receive output.
AppStdoutShareMode (integer) -
ShareModeargument for the output.
AppStdoutCreationDisposition (integer) -
CreationDispositionargument for the output.
AppStdoutFlagsAndAttributes (integer) -
FlagsAndAttributesargument for the output.
AppStderr (string) - Path to receive error output.
AppStderrShareMode (integer) -
ShareModeargument for the error output.
AppStderrCreationDisposition (integer) -
CreationDispositionargument for the error output.
AppStderrFlagsAndAttributes (integer) -
FlagsAndAttributesargument for the error output.
In general it is advisable to set both AppStdout and AppStderr in order to log output, as applications may log informational and error messages separately.
It is possible to direct both stderr and stdout to the same path but due to a limitation with nssm you must provide the exact same string in both the AppStdout and AppStderr registry values. Only if the two entries are the same will nssm be able to interleave the two streams.
As of version 2.11, nssm respects the AppEnvironment registry value supported by srvany. To specify a list of environment variables to pass to the monitored application, create a multi-valued string (REG_MULTI_SZ) value HKLM\System\CurrentControlSet\Services\servicename\Parameters\AppEnvironment where each entry is of the form KEY=VALUE.
It is possible to omit the VALUE if you just want the environment variable KEY to exist but the = symbol is mandatory. The service will not run if an invalid environment is specified!
For compatibility with srvany, the environment variables specified in AppEnvironment will replace those set by the system at service startup. Since that probably isn't what you want, use AppEnvironmentExtra instead.
As of version 2.19, nssm also respects the AppEnvironmentExtra registry value, which should have the same format as AppEnvironment. Environment variables set in AppEnvironmentExtra will be added to the service's default environment.