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.
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.
# 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; }
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.
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__