in reply to Re^3: Turn a perl script into a Win32 Service
in thread Turn a perl script into a Win32 Service

Even unbuffered output to STDERR in a BEGIN block doesn't show up

Where are you expecting the ouput to "show up"?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."
  • Comment on Re^4: Turn a perl script into a Win32 Service

Replies are listed 'Best First'.
Re^5: Turn a perl script into a Win32 Service
by Limbic~Region (Chancellor) on Oct 14, 2008 at 15:30 UTC
    BrowserUk,
    Here is an idea of what I am talking about (actual code is at home):
    #!/usr/bin/perl BEGIN { open(STDERR, '>', 'C:/app_name/err.log' or die $!; select STDERR; $| = 1; # un-necessary, I know print STDERR "hello world\n"; } use lib 'C:/app_name'; use strict; use warnings; use My::App; my $app = My::App->new(cfg => 'C:/app_name/app.cfg') or die "Didn't ge +t past this point\n"; print STDERR "Ok, got here\n"; while ($app->run_ok) { print STDERR "Inside while loop\n"; $app->do_things(); $app->rest_for_a_while(); } print STDERR "finishing up now\n";

    When I start the service, the err.log file is created but is 0 bytes. After 5 minutes or so, I stop the service and the following output shows up in the err.log file (hello world and Ok, Got here)

    Cheers - L~R

      The following works for me on XP:

      1. Install SrvAny with: instsrv "YourService" c:\the\Path\to\SrvAny.exe
      2. Use Regedit to add the following keys:
        [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SrvAny\Parameter +s] "Application"="\\perl\\bin\\wperl.exe \\test\\service.pl"

        Modify the paths to your wperl and script accordingly.

      3. Go into the Service Manager, double-click "YourService". Select the "Log-on" tab from the properties dialog and check the "Allow service to interact with desktop" option. Apply and save.
      4. This is my minimal service.pl:
        #! perl -slw use strict; use Win32; open LOG, '>', 'c:\test\service.log' or die; select LOG; $|++; Win32::MsgBox( 'From service.pl at ' . localtime, 0, "Service.pl" ); while( sleep 5 ) { Win32::MsgBox( 'From service.pl at ' . localtime, 0, "Service.pl" + ); print scalar localtime; } close LOG;

        It pops up a message box on startup and every 5 seconds thereafter. (And bloody annoying it is too :)

        It creates and writes to the log file.

      YMMV on Vista.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        BrowserUk,
        Thank you!

        There are two things here that I have not done (wperl vs perl and interact with desktop).

        Rather than try and apply those changes to my application, I will attempt to reproduce your results on Vista. If that doesn't work I will have isolated the problem to Vista. If it does, I will try it with my application. If it works then YAY. If it doesn't, I will have isolated my problem to my application. Either way, this is a huge help.

        Update: For the record, I did see the SrvAny trouble shooting guide indicating checking "interact with desktop" was necessary for the command prompt to be interactive but since my application doesn't read from STDIN or write to STDOUT/STDERR, I didn't think it applied.

        Cheers - L~R

        BrowserUk,
        I assume you named "YourService" SrvAny given your registry path.

        The Bad News

        Your working XP example did not work on Vista

        The Good News

        I know why - Vista no longer supports interactive services

        Where I am at

        The ActiveState PerlSvc is not free but I will check with sales to find out if it works on Vista. I still have Win32::Daemon to explore but it will be hard for me to tell which problems are related to the module versus Vista :-(

        Cheers - L~R