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>
You will be prompted to enter the path to the application you wish to run as a service and any commandline options to send to it.
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 Application and server is entered under Options.
Clicking Install service completes the installation of the service.
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.
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 is forced to forcibly terminate all processes and subprocesses belonging to the application.
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.