PREVIOUS  TABLE OF CONTENTS  NEXT 

Turning a Perl Program into an NT Service

Kevin Meltzer

One of the most frequently asked questions about Perl for Win32 is how to make a Perl program run as a Windows NT Service. An NT Service is like a Unix daemon: it's a long-lived background process that accommodates requests, or performs specific tasks. For example, web and FTP servers can be set up as NT Services: they stay running even when no user is logged on, and start up automatically when your computer boots. This article will give step by step instructions on how to run a Perl program as an NT Service.

What's Needed

There are two utilities needed to run a Perl program as a NT Service; srvany.exe and instsrv.exe (henceforth called srvany and instsrv). instsrv installs an instance of srvany as an NT Service; through the rest of the setup process described below, that instance of srvany runs the Perl program.

These utilities are included with the NT Resource Kit, and can also be found on the web. srvany can be downloaded at ftp://ftp.microsoft.com/bussys/winnt/winnt-public/ reskit/nt40/i386/srvany_x86.exe, and both instsrv and srvany can be downloaded from ftp://ftp.microsoft.com/ bussys/winnt/winnt-public/reskit/nt35/i386/. Those are the NT3.5 Resource Kit Utilities, but the srvany and instsrv you'll find there will work fine under NT 4.0.

The srvany program is what allows your Perl program to run as if it were an NT Service. Even though the Perl program itself isn't a "true" NT Service, it emulates one, and can be controlled as if it were one.

A Simple Example

Below is a brief program that monitors a directory, in this case c:\temp, for files with the extension .foo. After finding any such files, the program deletes them. This is a simple script, just meant to show how a Perl program used as an NT Service can repeat a task over and over. If you try this example out, create a file with the extension .foo and move it into c:\temp to see the Service in action.
    # c:\perl\lib\MyService.pl

    $dir = "c:\\temp";
    while (1) {
        opendir(DIR,$dir) || die "can't opendir $dir: $!";
        while(my $file = readdir(DIR)) {
            unlink qq($dir\\$file) if $file=~/\.foo$/i;
        }
        sleep(3);
        closedir DIR;
    } 

Installing the NT Service

Much of the setup process involves editing the NT Registry. If you do not feel comfortable doing this by hand, take advantage of the registry script provided below. The registry script creates all the values needed to run the Perl program as an NT Service. However, before editing the registry, srvany needs to be initialized by instsrv. The instsrv program starts srvany as an NT Service, and then after certain registry changes take effect, srvany will run your Perl program. The entire process can be done with the following nine steps.

1. To install srvany, open a command shell by clicking Start, then Run, and finally typing cmd. In the console window, type:

    INSTSRV NAME_OF_SERVICE C:\WINNT\SRVANY.EXE     

(If srvany is not located in C:\WINNT, this command needs to be changed accordingly.) This initializes an instance of srvany as an NT Service with NAME_OF_SERVICE as the Service name. NAME_OF_SERVICE might be the name of the Perl program being used ("example.pl"), or any other descriptor ("Monitor Temp Directory for foo extensions"). This name will appear under the Services application in the Control Panel.

1.5. The registry script shown later performs steps 2 through 7.

2. The next step is to edit the Windows NT Registry using RegEdit. This can be started by clicking on Start, then Run, and typing regedit.

3. Now locate the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\NAME_OF_SERVICE registry key. The key can be found by either maneuvering through the registry tree, or by searching for NAME_OF_SERVICE (Edit->Find). Right click on the key and choose New, then Key. This will create what looks like a new folder. Name the new key Parameters.

4. Right click on Parameters and select New, then String Value. In the space provided, type Application.

5. Again, right click on Parameters and select New, then String Value. In the space provided, type AppParameters.

6. Right click on Application and choose Modify, then type in the path to your Perl binary. For example, c:\perl\bin\perl.exe.

7. Next, right click on AppParameters and select Modify. Enter the path to the program you want run as a Service. There are no more required registry changes, so RegEdit can now be closed.

8. Now open the Control Panel and double-click the Services application. There should now be a service listed with the name of the service you chose in step one. The Startup option should be set to Automatic by default, but if it is not, click Startup and then Automatic if you want the Service to be started automatically when the machine boots. If you do not want it to start automatically, choose Manual. It is also best to have it log in as the System Account (which should be the default) because this way it will run no matter who is logged on to the machine, and even if no user is logged on. If appropriate, choose a user with the proper permissions for the application for the Log On As option. To get a little more information on Services, choose Start->Help->Find, type in Services and select "To configure startup for a service."

9. Finally, choose the Service from the list of Services, and click the Start button to launch it. The program is now running as an NT Service.

A Little Helper

Some may not feel comfortable with editing the registry by hand, so an alternative is to use this registry script to do it for you. Simply edit the areas noted in the comments, make sure the file has a .reg extension, and double click it to run.

Summary

With a little tweaking, the above example can be made into a useful application. For instance, it could be modified to monitor network traffic, parse incoming email, or maintain a directory in a more useful way than simply removing the occasional offending file.

I do suggest reading the documentation that comes with srvany, as well as what Microsoft has to say about the use of srvany. For example, Microsoft suggests stopping all Services running via srvany before any Service Pack installations. After following the instructions in this article, take a few moments to browse http://www.microsoft.com and Microsofts' Knowledge Base at http://search.microsoft.com/us/ (Choose "Support & the Knowledge Base" when searching for keywords).

__END__


PREVIOUS  TABLE OF CONTENTS  NEXT