Monday, June 22, 2009

Running any program as a windows service on Windows XP (or its many siblings).

I needed to run a perl script as windows service.

The perl module Win32::Daemon written by Dave Roth was not available on CPAN, and the one provided on his website seem to require a specially compiled version of perl. I'm running perl using the strawberry perl distribution on Windows XP, so Roth's module, even though it seemed like the best solution (it provides all those nice callbacks), had to be scrapped for my purpose at the moment.

After googling around I made the decision to run the service using the bare-bones instsrv.exe and srvany.exe.

Here are the steps necessary:

  1. If you don't have them yet, get instsrv.exe and srvany.exe by downloading the windows resource kit (google for srvany.exe and it should tell you from which Microsoft webste can you get it). Install the kit and it should give you instsrv.exe and srvany.exe. I copied instsrv.exe and srvany.exe from the resource kit's program files directory to my C:\Windows\System32 directory.

  2. Register your windows service by doing the following:

    instsrv yourservicename c:\Windows\System32\srvany.exe

    This will create a registry entry for the "yourservicename" service.

    Next you'll need to tell srvany which script to run when it starts your service.

  3. Now run regedit and (do heed the obligatory warning and cautions about directly editing registry entries on windows, in other words, close one eye and proceed :) specify the location of your script by adding the following keys:

    Entry (create if it doesn't exist):


    Keys (all are string) Value

    Application c:\strawberry\perl\bin\perl.exe
    AppParameters c:\Path\To\Your\Script

    This is optional:

    AppDirectory c:\Path\To\Your\App\Start\Directory

  4. Now go to Control Panel/Administrative Tools/Services. You service should be listed there. By default the service with the privileges of the Local System Account, so be aware of the privilege limitations.

  5. Profit!!!


Post a Comment

<< Home