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 script as a service
You are not restricted to running standard executables as services. nssm can also manage scripts. Just configure the path to your bat or cmd script and it will run correctly.
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 is insufficient. 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 where X:\path\to is the place where you "installed" nssm.
Note that unlike srvany, nssm considers failure to change to the directory specified in the AppDirectory registry setting as an error.
If the AppDirectory is not accessible to nssm, the service will fail to start. srvany, on the other hand, will start but will be run from %SYSTEMROOT% instead. nssm will log an error to the event log if the configured directory is invalid.
Remember that network drives are configured on a per-user basis and that services, unless explicitly configured otherwise by you, run under the LOCALSYSTEM account. Attempting to configure a network drive, eg z:\ as a service's AppDirectory will probably fail. UNC paths of the form \\server\share are OK.