Limbic~Region has asked for the wisdom of the Perl Monks concerning the following question:

All,
I have developed a Win32 application that doesn't require any human interaction. Initially, I wanted to find a way to "background" this application so that a logged in user wouldn't accidently interfere with the process but then I realized I needed more than just making it invisible.

I want the process to start automatically when the machine restarts. I want an administrator to be able to start, stop, and disable it while it is invisible. In essense, I want a windows service. I had hoped Win32::Service did more than allow you to manage existing services but apparently not. I then looked at Win32::Daemon. Unfortunately, the CPAN module was last updated in 2001. Fortunately, this is one of the Dave Roth modules so I looked there for an updated version - apparently the 5.10.x version is in beta and requires manual installation.

Cross posted at Win32::Daemon forum

Cheers - L~R

  • Comment on Turn a perl script into a Win32 Service

Replies are listed 'Best First'.
Re: Turn a perl script into a Win32 Service
by ikegami (Patriarch) on Oct 09, 2008 at 16:07 UTC

    Unfortunately, the CPAN module was last updated in 2001.

    It's not like the services API have changed much if any since then. It may not have needed changes since then.

    Does anyone know of an alternative to Win32::Daemon for making a program run as a service?

    Microsoft provides the SrvAny tool.

    And oddly enough, the link you provided for Win32::Daemon actually finds Win32::Daemon::Simple, not Win32::Daemon.

    Update: s/SvrAny/SrvAny/ as per reply.

      For the record, and to make the thread properly searchable, the name of the tool is Srvany, not Svrany. Note my comment has nothing to do with the capitalization. The problem is swapped letters. Again, I'm not trying to be a pedant; just getting 'Srvany' into the fossil record here. Of course the link was right, so nobody is going to get lost once they do find this thread.
      ikegami,
      It's not like the services API have changed much if any since then. It may not have needed changes since then.

      Well, assuming the CPAN module from 2001 was mature and feature complete then, this might be ok. In looking at the docs from the one on CPAN and the docs on Dave's website, the module has changed quite a bit. In fact, the beta release for 5.10 has gone to XS. I only commented on the age of the CPAN module for a couple of reasons. I didn't want someone to point me where I had already been and also to highlight this module seems to be being actively developed independent of CPAN.

      Microsoft provides the SvrAny tool.

      Thanks. I will have to look into that.

      Regarding the link not pointing to the right place - I think that doesn't have anything to do with me or my post but thanks for pointing it out.

      Cheers - L~R

      ikegami,
      I ended up going with the SrvAny tool. I wanted to share the following for those who might come across this post in the future.

      There doesn't seem to be a resource toolkit for Vista available for download. You can use the 2003 toolkit but you will get an error indicating "known compatibility issues". Additionally, you will get a cryptic "permission denied - contact your system administrator" even if you are logged in as an administrator. You will need to launch the command prompt with administrator rights. Finally, modifying the registry leaves quite a bit to be desired. If you need to have your service dependent on another service or some other "advanced" configuration - look at another service and use it as a starting point.

      Cheers - L~R

        All,
        It looks like SrvAny is a bust. The service starts and perl is in the process table, but it doesn't do anything. Even unbuffered output to STDERR in a BEGIN block doesn't show up until after stopping the service and then that's all it does. Still looking for solutions.

        Cheers - L~R

Re: Turn a perl script into a Win32 Service
by Bloodnok (Vicar) on Oct 09, 2008 at 17:27 UTC
Re: Turn a perl script into a Win32 Service
by tokpela (Chaplain) on Oct 10, 2008 at 08:59 UTC
    >> Does anyone know of an alternative to Win32::Daemon for
    >> making a program run as a service?

    I have used the PerlSvc program in the ActiveState PDK to develop a Windows Service and found it very straight-forward.

    >> Does anyone know of a "how to" guide to turn an
    >> completed program into a service after the fact?

    There is also a full chapter about developing a Windows service in David Roth's book (although this is definitely old):

    Win32 Perl Scripting: The Administrator's Handbook

    Finally, there is an example Windows service script from the Roth Consulting script repository:

    http://www.roth.net/perl/scripts/scripts.asp?DirMon.pl
      tokpela,
      Thanks for letting me know about PerlSvc. I think I will end up using that or M$'s tool rather than try and integrate the code.

      Regarding the "how to" guide advice, that wasn't quite what I was looking for. I was already familiar with the documentation about creating a service but that's not what I was after. I have already created a finished application and want advice on converting it. That's a lot different than starting with a blank slate.

      It is probably good that you provided links here though as it will likely help someone who stumbles on to this thread in the future.

      Cheers - L~R

Re: Turn a perl script into a Win32 Service
by NetWallah (Canon) on Oct 10, 2008 at 05:17 UTC
    Here is some code I wrote years back, to turn MRTG config files (which are fed into the perl MRTG script) into services.

    I originally wrote this because FireDaemon was no longer free, so you will see conversion code here.

    As a minimum, this code will show you the registry hooks and configuration parameters for services.

    Feel free to modify and (ab)use.

         Have you been high today? I see the nuns are gay! My brother yelled to me...I love you inside Ed - Benny Lava, by Buffalax

Re: Turn a perl script into a Win32 Service
by jdporter (Paladin) on Oct 10, 2008 at 19:20 UTC
Re: Turn a perl script into a Win32 Service
by xiaoyafeng (Deacon) on Oct 12, 2008 at 07:41 UTC
    You might have a try rktools

    I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction
Re: Turn a perl script into a Win32 Service
by bart (Canon) on Oct 14, 2008 at 15:00 UTC
      bart,
      As I have explained, I already figured out how to get the application to start as a service using SrvAny despite the problems with Vista that no resource in this thread so far has addressed (which is why I added the response). Unfortunately, it still doesn't work.

      In a nutshell, there appear to be 3 options:

      • Microsoft's SrvAny (free but broken)
      • ActiveState's PerlSvc (expensive and uknown if it works with 5.10 and Vista)
      • Roth's Win32::Daemon (free but with drawbacks)
      Those drawbacks being that it is in beta for 5.10 (converted to XS). I don't know for sure that it will work on Vista and it doesn't seem straight forward to convert an existing application.

      The last drawback is really a personal one. I am sure it wouldn't take me "too long" to shoe horn it in but I would really like a turn key solution.

      Cheers - L~R

Re: Turn a perl script into a Win32 Service
by wufnik (Friar) on Jan 07, 2009 at 10:18 UTC
    hello -
    you should check out ActiveState's PerlSvc, alas a commercial offering, but it works nicely, mostly.
    best wishes,
    ...wufnik

    -- in the world of the mules there are no rules --
      wufnik,
      Thanks. PerlSvc was discussed a bit in this thread but I didn't post a reply containing my entire dealings with AS. While I didn't run into the same problems with PerlSvc as I did SrvAny on Vista + 5.10, my application still did not work as a service. It has to do with Win32::OLE and Outlook running as a service. My first email into AS technical support went unanswered. My second email was promptly answered as "we are still looking into it". A third email came back with "Dev has looked at it twice now and no one has any ideas. We distribute but don't support Win32::OLE so not even PDK devs are certain what's going on behind the scenes".

      Of course these exchanges with AS have been condensed for the purposes of this post but they are in line with the actual exchanges. So it seems that for the specific application I need, not even an expensive commercial product is able to solve the problem.

      Cheers - L~R

Re: Turn a perl script into a Win32 Service
by wol (Hermit) on Oct 14, 2008 at 15:12 UTC
    I've been trying out some of the options above, but I've encountered some issues.

    Win32::Daemon is working for me, but I'd like to use Win32::Daemon::Simple because otherwise there's a lot of skeleton code to write and, well, it's just simpler :-)

    Unfortuantely, it's throwing up some errors as soon as I try to use it:

    C:\tmp>perl use Win32::Daemon; use Win32::Daemon::Simple; Global symbol "$HKLM" requires explicit package name at C:/APPS/Perl/s +ite/lib/Win32/Daemon/Simple.pm line 66. Global symbol "$HKLM" requires explicit package name at C:/APPS/Perl/s +ite/lib/Win32/Daemon/Simple.pm line 78. Global symbol "$HKLM" requires explicit package name at C:/APPS/Perl/s +ite/lib/Win32/Daemon/Simple.pm line 276. BEGIN not safe after errors--compilation aborted at C:/APPS/Perl/site/ +lib/Win32/Daemon/Simple.pm line 494. Compilation failed in require at - line 2. BEGIN failed--compilation aborted at - line 2.
    The Win32::Daemon::Simple code is using a variable $HKLM (when attempting to talk to the Windows registry) but it's not defined in Simple.pm.

    Any ideas why this is fouling up? Where is this variable supposed to come from?

    One obvious area for consideration is module incompatibilites, so here are the versions I'm using:

    Win32::Daemon $VERSION = 20030617; Win32::Daemon::Simple $VERSION = '0.2.6'; Win32::Registry $VERSION = '0.07';
    <fingers crossed...>

    --
    .sig : File not found.

      wol,
      First, where did you get Win32::Daemon from? The one on CPAN is very old - you should get the one directly from Roth's site (links elsewhere in the thread) though I don't know if Win32::Daemon::Simple will still work with it.

      Second, what's your configuration? I am specifically interested in AS 5.10 on Vista but have XP with SP3 to test with as well. If you are not familiar with Roth's forums for his modules, you should cross post this there (which is what I did for my question - link in the root node).

      Third, thank you so much for actually trying to get this to work rather than just point to resources that aren't working. I don't mean that as a slight to others who have contributed but I have explained on a number of occassions that SrvAny just isn't working and that converting an existing application over to Win32::Daemon isn't as simple as use Win32::Daemon; (looking for a "how to" guide).

      Cheers - L~R

        OK - lots of little questions to answer...

        Yes, the Win32::Daemon module came direct from Roth Consulting's Official Win32::Daemon Home Page, using the dead simple instructions there:

        If you have ActivePerl (aka Perl from ActiveState Tool Corp. version 5.005 or higher) or if you have Core Perl 5.005 compiled with the PERL_OBJECT macro defined you can auto download and install the extension. You need to run the Perl Package Manager script which comes with Perl in the perl\bin directory:
        perl ppm.pl install http://www.roth.net/perl/packages/win32-daemon.ppd
        This will automatically download and install the latest version.
        I'm running wih Perl v5.8.7 (build 813 from ActiveState) on Win XP.

        I've not cross-posted to the Win32::Daemon forms there, but I'll try tomorrow (time to go $home now).

        I don't know if Win32::Daemon::Simple will still work with it.
        That would appear to be the million dollar question (but remember that the value of your question can go down as well as up).

        Until tomorrow...

        --
        .sig : File not found.