NSSM - the Non-Sucking Service Manager

Use cases

Managing a service which should run constantly

Simply install the service as described in the usage notes. Should your application ever fail or crash, nssm will attempt to start it up again.

Running a tool or script once at system startup

Install the service then open the registry and configure the action on exit to Ignore as described in the usage notes. If you want nssm to retry your script until it succeeds, create a registry entry corresponding to a success exit code and set that to Ignore, leaving the default at Restart.

Customising the action taken when a service fails

Please note that the steps required are slightly different for versions of Windows prior to Vista.

The Windows service manager is capable of taking different actions when a service exits. See the Recovery tab of the service's property window for more details. Windows can restart the service, call a program or script or even restart the computer. By default, however, it will take no action and cannot take any action if it doesn't know the application has actually stopped, which is the big weakness of srvany.

Install a service and set the action on exit to Exit to have nssm reliably report service failure then configure your preferred service recovery action through Windows.

If you are using a Windows version prior to Vista the above will not work. Even if nssm reports that the service stopped with an error the service manager will not apply a recovery action because it considers the service to be gracefully stopped. You must set nssm's exit action to Suicide to have it simulate a crash if you want the service to be recovered. If you want a recovery action to be taken even when the application exits gracefully you must explicitly set the action on exit code 0 to Suicide as well.

Using nssm in place of srvany

If you are already using the combination of instsrv and srvany to run your service, nssm can almost be used as a drop-in replacement. Stop your service then edit the registry, changing HKLM\System\CurrentControlSet\Services\servicename\ImagePath to X:\path\to\nssm.exe run where X:\path\to is the place where you "installed" nssm. Don't forget to add run after the path! This is there so if you accidentally run nssm without any arguments it won't just sit there trying to interact with a non-existent service database...