punkish has asked for the wisdom of the Perl Monks concerning the following question:

I am stumped with a non-responsive Win32::Daemon service on my hands. (background: I am using ActiveState Perl 5.8.4 on a WinXP box).

First, I created a complicated perl script as a Win32 service. The service installs perfectly, but fails to start with "Error 1053: Could not start the ComplicatedTask on Local Computer". The Event Viewer says "Timeout (30000 milliseconds) waiting for the ComplicatedTask service to connect."

Then I pared down my script to a really simple perl script. Just a simple

print LOG "hello\n";

No cigar. Still the same errors as above.

I have read the node Win32::Daemon and put the little

BEGIN { open( STDERR, ">>D:/htdocs/myservicelog.err" ) or die "invisible err +or"; warn "$0 started ".localtime().$/; }

as prescribed by tye. The log gets a dutiful

complicatedtask.pl started Mon Dec 27 14:38:24 2004 etc.

My code is in a standard Win32::Daemon service control loop as in

open LOG, ">>mylogfile.txt" or die; # Start the service control loop Win32::Daemon::StartService(); while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) ){ if( SERVICE_STARTING == $State ) { # some service startup code ...... Win32::Daemon::State( SERVICE_RUNNING ); } elsif( SERVICE_PAUSE_PENDING == $State ) { Win32::Daemon::State( SERVICE_PAUSED ); next; } elsif( SERVICE_CONTINUE_PENDING == $State ) { Win32::Daemon::State( SERVICE_RUNNING ); next; } elsif( SERVICE_STOP_PENDING == $State ) { # some service shutdown code ............ Win32::Daemon::State( SERVICE_STOPPED ); next; } elsif( SERVICE_CONTROL_SHUTDOWN == $State ) { # Request x seconds to shutdown... Win32::Daemon::State( SERVICE_STOP_PENDING, 45 ); .............. Win32::Daemon::State( SERVICE_STOPPED ); next; } elsif( SERVICE_RUNNING == $State ) { doMyStuff(); sleep 30; } else { # Un-handled control messages Win32::Daemon::State( SERVICE_RUNNING ); } sleep 5; } Win32::Daemon::StopService(); sub doMyStuff { print LOG "hello\n"; }

What now? Is there any other error log that I can turn on that actually reports on _why_ the service didn't respond in a timely fashion?

Btw, the task script works fine when not installed as a service.

Replies are listed 'Best First'.
Re: Win32::Daemon installed service fails to start
by astroboy (Chaplain) on Dec 27, 2004 at 21:35 UTC

    In my experince Win32::Daemon can be difficult to debug when trying to get something working. Often the errors are due to the environment not being correct (are you running the service afrom the local system account or do you log on as a user?

    In addition, I tend to use Log::Log4perl a lot when using Win32::Daemon. I can wind the logging "noise" back down when I'm happy that everything is running

      Often the errors are due to the environment not being correct (are you running the service afrom the local system account or do you log on as a user?

      Very likely, and that is what I want to determine, but don't know how. In response to your question -- I tried both, local system account and as myself. No luck either way. Actually, I pared down my simple script to an even simpler script. I mod-ed it to do nothing when service is running... just go to sleep for 30. Nada. No change whatsoever. It just simply fails to start.

Re: Win32::Daemon installed service fails to start
by dws (Chancellor) on Dec 27, 2004 at 21:50 UTC

    What now? Is there any other error log that I can turn on that actually reports on _why_ the service didn't respond in a timely fashion?

    Check Settings | Control Panel | Administrative Tools | Services. Double-click on your service, and select the Log On tab, and check that your service is logging on with sufficient privs to listen on port 80.

    Oops: I misread Win32::Daemon as HTTP::Daemon, and answered a question that wasn't asked.

      check that your service is logging on with sufficient privs to listen on port 80

      Hmmmm... there is no port 80 involved here. This is not a web service. Just a plain, simple service that wakes up, does nothing, then goes to sleep for 30. The service is supposed to be logging on using the Local System Account.

Re: Win32::Daemon installed service fails to start
by osunderdog (Deacon) on Dec 27, 2004 at 23:21 UTC

    I haven't written a service in a while (and when I did it was in C...) One thought I had is that your open might not be in a good place. I would do the open in the SERVICE_RUNNING state and close it after doMyStuff. ALSO, you might check to make sure your prints aren't being buffered, so put $|++; at the top.

    Also, code running as a service doesn't have access to drive mappings that you do. If you are trying to write to anything other than the local machine, then you will have to run the server as a user.


    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


    OSUnderdog
      No, it has nothing to do with open or print. I actually pared down the script to the barest minimum possible (pseudo code follows)
      start service sleep for 30 stop service
      The darn service just doesn't start. Google revealed a thread on ASPN's forum -- someone there was having a similar, inexplicable problem with a WinXP machine, while the exact same script was running fine on a Win2k box. Others reported back that they had no problems on WinXP. I am wondering if I have been hit by some strange WinXP related bug. Tomorrow I will see if I can try the script on a Win2k box.

      Very frustrating.

Re: Win32::Daemon installed service fails to start
by osunderdog (Deacon) on Dec 28, 2004 at 16:18 UTC

    punkish, It seemed to work for me. I used sc to do the install. Here is the script I registered along with a transcript of what I did after the __END__:

    As I suspected, LOG is being buffered. So it didn't show up until after I stopped the service.

    Hope this helps.


    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


    OSUnderdog
      osunderdog++

      I had no idea about the SC.exe utility. I tried your way above, and, guess what, it works. I have a service now running correctly. Would still love to know what was causing the error in the first place, but at least I can move now.

      Many thanks, and have a great new 2005.

        Great. Glad that worked for you. Win Services are extremely finicky and there is really no good way to debug them. Services run under the administrator account so they don't neccesarily have the same path as a user. So you have to absolute path everything.

        I forgot that sc isn't a common utility. I used to use it all the time when telnetted to a windows box. It was the only decent way to remotely control services.

        Anyway, glad I could help.


        "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


        OSUnderdog
Re: Win32::Daemon installed service fails to start
by bibo (Pilgrim) on Dec 28, 2004 at 18:11 UTC
    I feel your pain. I have no experience with the module mentioned, so this may all be worthless...

    In the services which I have written (in C/C++), I have always observed that the ServiceMangler API needs to be reassured that your service is doing something. I believe your service will startup better if you send lots of SERVICE_START_PENDING messages using the appropriate plumbing. Send them when you first begin to start up. Send them in your startup loop and b etween calls to other subs or functions. Pepper them liberally during the time-consuming parts of your code.

    Then the SM feels like you are making progress and actually not dead in the water. Best of luck.

    --bibo