Configuring an Executable to Run as a Service on Windows Server.
Being able to run an executable as a service can be very useful. This example uses ip_relay.exe, a command-line tool (with a Win32 equivalent) that can be used to relay one IP/Port to another.
Two tools are required to install a standard executable as a service, INSTSRV and SRVANY. Both are available in the Windows 2003 Server Resource Kit which can be downloaded from Microsoft here.
If you don't want to install all of the tools - or you want to run them on Windows 2000 Server - you can download the MS Resource Kit and extract the individual files using an archiving tool such as 7-Zip.
The paths for the files (once you have extracted the rktools.exe file) are:
Srvany.exe is the executable that will run your executable as a service. You could try running the executable directly but depending on the executable, it could result in orphaned processes or it might just return an error. The worst (or maybe best) case is that the executable will not run at all.
Instsrv.exe is the executable that used to install srvany.exe as a service.
Installing the Service.
First, install srvany.exe as a service. From a command prompt, run
%path-to%\INSTSRV.EXE ServiceName %path-to%\SRVANY.EXE.
For this example: c:\INSTSRV.EXE "IP Relay" c:\WINNT\SRVANY.EXE. I've wrapped the service name in quotes as this service name has a space in it.
Once you've installed the service, you'll need to edit the system registry to point to the actual executable file, add any switches or options, and add a nice description so that when you come back to it in six months time you'll remember what it's for.
Serious problems might occur if you modify the registry incorrectly. Back up the registry before you modify it, then you can restore the registry if a problem occurs. For more information about how to back up and restore the registry, click the following link to view an article in the Microsoft Knowledge Base on this subject: KB322756 (http://support.microsoft.com/kb/322756/) How to back up and restore the registry in Windows.
Use Regedit.exe to edit the registry key:
"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\IP Relay-(Your service name here!)"
- Right-Click on the Service name, select "New > Key" and name the key "Description"
- Double-Click "Description" and add a good description for the service.
- In this example:"Port XX Forward for Admin Console access from localhost to 192.168.1.x"
- Right-Click on the Service name, select "New > Key" and name the key "Parameters"
- Right-Click on the Parameters key, select "New > String Value" and name the String Value "Application"
- Double-Click "Application" and add the full path to the executable that you want to run as a service.
- In this example the full path is "C:\Program Files\ip_relay_1.1\bin\win32\ip_relay.exe" XX 192.168.1.x XX; the path to the executable is in quotes so that the options can be added. Just in case you're curious, the options shown here are:
- Local Port Number
- Destination IP address
- Destination Port Number
You can now use the Services MMC snap-in or NET START SERVICE NAME from a console to start your new service!