Pages

Wednesday, January 4, 2012

Run Perl script as a windows service

If you are looking for “How to run a Perl script as a windows service, so it can be run as a background service”, then you are at right place. Here are some generalized steps that can be used to setup Perl scripts as services under Windows 7. As it works on windows7 it should work with other version of windows, not tested though.

Put NT resource kit in place:
Copy the window NT resource kit to C:\WINNT\. You can name it as you like but putting it as WINNT will help you to recall in future. The Windows NT Resource Kit provides two utilities "INSTSRV.EXE" and "SRVANY.EXE" that allow you to create a Windows NT user-defined service for Windows NT applications.

INSTSRV: - Allows you to installs and removes system services from Windows NT.
SRVANY: - Allows any Windows NT application to run as a service.

Download INSTSRV and SRVANY

Add a new service using command line: 
Run MS-DOS command prompt as an administrator as shown below, and run the following command:

INSTSRV <Service Name> C:\WINNT\SRVANY.EXE
For example: INSTRV MyPerlService C:\WINNT\SRVANY.EXE

Above command will register the “service” with service name “MYPerlService”. Following is the screenshot for your reference.


Now go to “Control Panel\All Control Panel Items\Administrative Tools\Services” and set the "Logon As" to Local System Account in the service properties (ignore if it is already set).

Modify registry to add perl script in service:-
Run MS DOS command prompt and type command “REGEDIT”. It will pop up a window. Follow the path below and look for your service name “MyPerlService”:

HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Services -> MyPerlService

Add the new Key: Parameters
Select Parameters
Add the String Value: Application
String: C:\PERL\bin\perl.exe     (your path to the perl.exe program) 

Add the String Value: AppParameters
String: C:\DIR_To_Perl\perl_script.pl   (your path to the perl script)


Finally the configuration for your service should look like below:-

Exit the Registry Editor.

Start the service:-
Start the service from “Control Panel\All Control Panel Items\Administrative Tools\Services”



To Stop & Remove the service
To stop the service, go to the above path and select “Stop”. This service can be removed from registry with the help of below command.
INSTSRV MyPerlService REMOVE 
Above command will mark this service as deleted and will be removed from the services list on next start of your system.

That’s all and you have configured your perl program to run as a windows service. You can add any number of processes using these steps.


3 comments:

  1. Thanks for your post. It is definitely a good alternate to the missing Win32 Daemon Perl...

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Apparently.. the windows service does not work when inside the Perl script there is a system(...) command. It is shown as running, but the script does not execute (does not even run the command inside the system function). How can I fix that?

    ReplyDelete