Running any program as a Windows NT service

Running a program as a service, ie. starting it when the system starts, do not finishing it when there is not any user logged in, and do not interacting with any user at all, is a simple and easy to do task in *nix. More or less it consists in running the program in background and doing a little bit more.

The problem was to run a program as a service in MS Win. It implies designing and coding your program in a particular way, using the OS API.

I had developed an ad-hoc server for mocking some services over a custom protocol, and I needed to run it on win in background mode, even when I was not logged in. It was coded in Java, and I had no idea about developing a win service. So I looked for a solution… and I found it.

For those who are not interested in using Win API, and still need to run their own program as a service, the solution is Nick Rozanski’s SrvStart.

A really magical solution that does the job.

First dowload it at http://www.rozanski.org.uk/software

Then follow the instructions at http://rozanski.org.uk/services_quick

From the instructions page…


a)
Let's assume that you want to run the following command:

C:\MYPROG\MYPROG.EXE -a -b -c xxxx yyyy zzz

as a Windows NT service called MY_SERVICE.

b)
Unpack the SRVSTART executables to a directory in your $PATH:
srvstart.exe - SRVSTART executable
srvstart.dll - SRVSTART library
logger.dll - LOGGER library
(If you're not sure where to put these files, just unpack them to C:\WINNT\SYSTEM32).

c)
Create a text file called, say, C:\SRVSTART\SRVSTART.INI.

[MY_SERVICE]
startup=C:\MYPROG\MYPROG.EXE -a -b -c xxxx yyyy zzz

d)
Open a Command Prompt in Windows NT. Install your service by typing the following command:

srvstart.exe install MY_SERVICE -c C:\SRVSTART\SRVSTART.INI

Just take a look at it!