http://www.perlmonks.org?node_id=999758

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

I am humbly searching for an answer to a Win32::Daemon problem that has been filling my System Logs with errors. I created a service using the CPAN different callback routines framework. After super searching the monastery I found one thread that covered the topic, but did not provide a solution - "Win32::Daemon::State returns a 0 or 1" from 2009. I appreciate any wisdom that can be given in this matter.

use strict; use Fcntl qw(:DEFAULT :flock); use File::Copy; use Net::FTP; use Win32; use Win32::Daemon; use IniInstall; use CopyFiles; use ftpsend; open (STDERR, ">>c:/error.err") or die "invisible error"; warn "$0 started ".localtime().$/; my $ServicePath = 'c:\Foo\FooBar.exe'; Win32::Daemon::RegisterCallbacks( { start => \&Callback_Start, running => \&Callback_Running, stop => \&Callback_Stop, pause => \&Callback_Pause, continue => \&Callback_Continue, } ); my %CONTEXT = ( last_state => SERVICE_STOPPED, start_time => time(), ); # Start the service passing in a context Win32::Daemon::StartService( \%CONTEXT); sub Callback_Running { my ( $Event, $CONTEXT ) = @_; if( SERVICE_RUNNING == Win32::Daemon::State() ) { my @ltime=localtime; $main::MINUTE = sprintf("%02d",$ltime[1]); $main::HOUR = sprintf("%02d",$ltime[2]); $main::DAY = sprintf("%02d",$ltime[3]); $main::MONTH = sprintf("%02d",($ltime[4]+1)); $main::YEAR = sprintf("%02d",(($ltime[5])+1900)); my %HOURARR; my $HHARR=$ltime[2]; foreach ("06","12","18") { $HOURARR{$_}=1; }; if (( exists $HOURARR{$HHARR}) and ($main::MINUTE == 00)) { CopyFiles::transfer; } if (( $main::HOUR == 23 ) and ($main::MINUTE == 59)) { sleep (90); CopyFiles::transfer; } if (( $main::HOUR == $main::hh ) and ($main::MINUTE == $main::mm)) { ftpsend::send; }#endif } 1; } sub Callback_Start { my ( $Event, $CONTEXT ) = @_; # Initialization code $CONTEXT->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); IniInstall::Initiate; } sub Callback_Pause { my ( $Event, $CONTEXT ) = @_; $CONTEXT->{last_state} = SERVICE_PAUSED; Win32::Daemon::State( SERVICE_PAUSED ); } sub Callback_Continue { my ( $Event, $CONTEXT ) = @_; $CONTEXT->{last_state} = SERVICE_RUNNING; Win32::Daemon::State( SERVICE_RUNNING ); IniInstall::Initiate; } sub Callback_Stop { my ( $Event, $CONTEXT ) = @_; $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(); } # Check for any outstanding commands. Pass in a non zero value # and it resets the Last Message to SERVICE_CONTROL_NONE. if( SERVICE_CONTROL_NONE != ( my $Message = Win32::Daemon::Las +tControlMessage(1) ) ) { if( SERVICE_CONTROL_INTERROGATE == $Message ) { Win32::Daemon::State(my $CONTEXT->{last_state} ); } elsif( SERVICE_CONTROL_SHUTDOWN == $Message ) { Win32::Daemon::State( SERVICE_STOP_PENDING, 25000 ); } }