Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Win32::Daemon service doesn't reach RUNNING state

by SwaJime (Scribe)
on May 28, 2019 at 15:48 UTC ( #11100647=perlquestion: print w/replies, xml ) Need Help??

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

Added: Strawberry v5.28.1

Added: Win32::Daemon v20131206

I have an implementation of Win32::Daemon that is working on some systems and not on others. The only common thread I've found so far is that the bulk of systems it is not working on are Windows Server 2012 systems.

The system I am currently looking at though is Windows Server 2016 Standard 1607 14393.2969 64-bit with Strawberry Perl

The state starts at SERVICE_STOPPED, switches to SERVICE_NOT_READY, and then stays there. The state never switches to SERVICE_RUNNING

Nothing in the Callback_start is printed.

To run:

cd \Users\john\test perl test-svc.pl --install net start TestService

I've stripped down the program to one file as shown below to help troubleshoot.

#!/usr/bin/env perl use strict; use warnings; use File::Basename; use Cwd qw(abs_path getcwd); use Win32; use Win32::Daemon; use Cwd qw(abs_path getcwd); use File::Spec::Functions; # Get command line argument - if none passed, use empty string my $opt = $ARGV[0] || ""; my $path = $ARGV[1]; open(my $fh, ">>", "C:\\Users\\john\\test\\test.log"); select($fh); $|=1; print $fh "Running Test Service.\n"; print $fh "opt: $opt\n"; print $fh "path: $path\n"; # Check command line argument if ($opt =~ /^(-i|--install)$/i) { install_service(); } elsif ($opt =~ /^(-r|--remove)$/i) { remove_service(); } else { if (!$path) { print $fh "This program is intended to be run as a service.\n" +; exit 1; } chdir $path; Win32::Daemon::RegisterCallbacks( { start => \&Callback_Start, running => \&Callback_Running, stop => \&Callback_Stop, pause => \&Callback_Pause, continue => \&Callback_Continue, } ); print $fh "Debug 1\n"; my %Context = ( last_state => SERVICE_STOPPED, start_time => time(), ); print $fh "Debug 2\n"; Win32::Daemon::AcceptedControls(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_PAUSE_CONTINUE | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_PARAMCHANGE | SERVICE_ACCEPT_NETBINDCHANGE); print $fh "Debug 3\n"; Win32::Daemon::StartService( \%Context, 1 * 1000); print $fh "Debug 4\n"; close STDERR; close STDOUT; } sub Callback_Running { my($Event, $Context) = @_; print $fh "SERVICE_RUNNING: " . SERVICE_RUNNING . "\n"; print $fh "SERVICE_CONTROL_TIMER: " . SERVICE_CONTROL_TIMER . "\n"; print $fh "State: " . Win32::Daemon::State() . "\n"; if(SERVICE_RUNNING == Win32::Daemon::State()) { # if(SERVICE_CONTROL_TIMER == Win32::Daemon::State()) { print $fh "Checking in.\n"; $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); } } sub Callback_Start { my($Event, $Context) = @_; print $fh "Starting\n"; $Context->{last_state} = SERVICE_RUNNING; print $fh "DEBUG Set Context\n"; Win32::Daemon::State( SERVICE_RUNNING ); print $fh "DEBUG Set State\n"; } sub Callback_Pause { my($Event, $Context) = @_; print $fh "Paused.\n"; $Context->{last_state} = SERVICE_PAUSED; Win32::Daemon::State( SERVICE_PAUSED ); } sub Callback_Continue { my( $Event, $Context ) = @_; print $fh "Resumed running."; $Context->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); } sub Callback_Stop { my($Event, $Context) = @_; print $fh "Stopped.\n"; $Context->{last_state} = SERVICE_STOPPED; Win32::Daemon::State( SERVICE_STOPPED ); # We need to notify the Daemon that we want to stop callbacks and +the service. Win32::Daemon::StopService(); } sub install_service { my ($path, $parameters); my $dir = getcwd; # Get the program's full filename, break it down into constituent +parts my $fn = Win32::GetFullPathName($0); my ($cwd,$bn,$ext) = ( $fn =~ /^(.*\\)(.*)\.(.*)$/ ) [0..2] ; # Determine service's path to executable based on file extension if ($ext eq "pl") { # Source perl script - invoke perl interpreter $path = "\"$^X\""; # Parameters include extra @INC directories and perl script # @INC directories must not end in \ otherwise perl hangs my $inc = ($cwd =~ /^(.*?)[\\]?$/) [0]; $parameters = "-I " . "\"$inc\"" . " \"$fn\" \"myflag\" \"$dir +\""; } else { # Invalid file type? die "Can not install service for $fn, file extension $ext not supported."; } my $sServiceName = "TestService"; # Populate the service configuration hash # The hash is required by Win32::Daemon::CreateService my %srv_config = ( name => $sServiceName, display => "Test Service ($sServiceName)", path => $path, description => "For debugging.", parameters => $parameters, service_type => SERVICE_WIN32_OWN_PROCESS, start_type => SERVICE_AUTO_START, ); # Install the service if (Win32::Daemon::CreateService(\%srv_config)) { print $fh "Test Service has been installed.\n"; } else { print $fh "Failed to install Test Service.\n"; } print $fh "Setting agent service to delayed start.\n"; print $fh "sc config $sServiceName start= delayed-auto\n"; print $fh `sc config $sServiceName start= delayed-auto` . "\n"; } sub remove_service { print $fh "Test Service is being removed.\n"; my $sServiceName = "TestService"; my $hostname = Win32::NodeName(); if (Win32::Daemon::DeleteService($sServiceName)) { print $fh "Test Service uninstalled successfully.\n"; } else { print $fh "Failed to uninstall Test Service.\n"; } }

Replies are listed 'Best First'.
Re: Win32::Daemon service doesn't reach RUNNING state
by footpad (Monsignor) on May 28, 2019 at 16:50 UTC

    I don't have a setup where I can easily try to reproduce your results, however, I notice that you don't do much with the return value of CreateService(). Per the module docs, you should be calling GetLastError() when CreateService() returns false. The underlying service docs go into a bit more detail about the data that might be returned. (You'll likely need to dig up the underlying constant values.)

    I'd start by trying to get more extended error info. That should lead you to the next step in your investigation.

    (One obvious question is "Are you running this as an admin?" The note on the CPAN doc (see link above) suggests that's a requirement.)

    Hope this helps...

    --f

      Thank, I will look into this. Meanwhile, I found more info: Changing Callback_Start as follows allows it to run, however the resume/contine callback function does not work and does not benefit from a similar change:

      sub Callback_Start { my($Event, $Context) = @_; print $fh "Starting\n"; print $fh "DEBUG Setting Context\n"; $Context->{last_state} = SERVICE_RUNNING; print $fh "DEBUG Setting State\n"; Win32::Daemon::State( SERVICE_RUNNING ); print $fh "DEBUG Setting State again\n"; Win32::Daemon::State( SERVICE_RUNNING ); }

        The error message you mentioned was not triggered in my case. Thanks for the detail, and I will add that to my code. But that is not the issue directly at hand.

      Also, yes, I am running as admin.

Re: Win32::Daemon service doesn't reach RUNNING state
by SwaJime (Scribe) on May 28, 2019 at 15:55 UTC
    Correction: Callback Start is printing out debug info. Here is the output:
    Running Test Service. opt: myflag path: C:/Users/u601501/test Debug 1 Debug 2 Debug 3 Starting DEBUG Set Context DEBUG Set State SERVICE_RUNNING: 4 SERVICE_CONTROL_TIMER: 4128 State: 1 SERVICE_RUNNING: 4 SERVICE_CONTROL_TIMER: 4128 State: 0 SERVICE_RUNNING: 4 SERVICE_CONTROL_TIMER: 4128 State: 0 SERVICE_RUNNING: 4 SERVICE_CONTROL_TIMER: 4128 State: 0

      I just got a chance to retry on one of the servers, this one also a Windows 2016 server, adding in that line did not help. On that system, no matter what I did, it appeared Callback_Running() was never triggered. I added a callback for "timer" and that did not work either. It was never triggered.

Re: Win32::Daemon service doesn't reach RUNNING state
by Anonymous Monk on May 29, 2019 at 01:14 UTC
    What does event log say?

      For the one that does run with the duplicated line but will not resume after pause:

      The Test Service (TestService) service entered the running state. The Test Service (TestService) service entered the paused state. The Test Service (TestService) service has reported an invalid current + state 0.

      Last message repeats over and over again after attempt to resume, until stopped.

      Will check the other system where running is never triggered as soon as possible.

        On yet another system, same code gives a different error:

        Undefined subroutine &Win32::Daemon::SERVICE_STOPPED called at ... and + is pointing to this code in the initial startup:
        my %Context = ( last_state => SERVICE_STOPPED, start_time => time(), );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11100647]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (9)
As of 2019-10-14 17:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?