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.


5 comments:

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

    ReplyDelete
  2. The Blog you are running is just awesome. The stuff and theme of the blog is so impressive. The actual match, like your blog, is found on very rare places. This blog is so clear and simple, there is no useless content you have added just to show a fake knowledge about the topic. I am new to blogging and picking lot from your blog. Here is my blog if you want to take a look and do suggest me if any problem or something missing.
    Thanks a lot!!!

    ReplyDelete
  3. Seems to be a perfect place to learn something new.. Here I can pick many things for my blog as I have recently created and its in progress. My blog is totally different from your blog. But still I want the admin to suggest me that how can I attract visitors to increase traffic. Have a look here, http://free-songs.org on my blog and do suggest me if there is any requirement for any change. I would be really glad and any suggestions would be appreciated. Thanks in advance!!!!

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

    ReplyDelete
  5. 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