-
Notifications
You must be signed in to change notification settings - Fork 7
Open
Description
On Windows 10 using Strawberry Perl version 5.30.1, Win32-Daemon version 20190315. When I try to run the program daemon.pl
supplied in the test
folder of the distribution:
use strict;
use warnings;
use Data::Dumper qw(Dumper);
use Win32::Daemon;
Win32::Daemon::StartService();
my %List;
my $SLEEP_TIMEOUT = 100; # This value is in milliseconds
my $START_TIME = time();
my ( $DB_DIR, $DB_FILE_NAME ) = ( Win32::GetFullPathName( $0 ) =~ /^(.*)\\([^\\]*)$/ );
$DB_FILE_NAME =~ s/\..*?$/.log/;
print "DB_DIR: $DB_DIR\n";
print "DB_FILE_NAME : $DB_FILE_NAME\n";
if( open( LOG, ">$DB_DIR\\$DB_FILE_NAME" ) )
{
my $StartTime = localtime( $START_TIME );
$| = 1;
print LOG << "EOT"
# Service Starting
# Script: $0
# PID: $$
# Date: $StartTime
EOT
}
Log( "Starting service" );
Win32::Daemon::StartService();
Log( "Entering service loop" );
our $LastState = SERVICE_STOPPED;
our $State;
while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) )
{
if( SERVICE_START_PENDING == $State )
{
# Initialization code
$LastState = SERVICE_RUNNING;
# Win32::Daemon::State( [ state => SERVICE_RUNNING, error => NO_ERROR ] );
Win32::Daemon::State( SERVICE_RUNNING );
Log( "Service initialized. Setting state to Running." );
}
elsif( SERVICE_PAUSE_PENDING == $State )
{
$LastState = SERVICE_PAUSED;
Win32::Daemon::State( SERVICE_PAUSED );
Log( "Pausing." );
next;
}
elsif( SERVICE_CONTINUE_PENDING == $State )
{
$LastState = SERVICE_RUNNING;
Win32::Daemon::State( SERVICE_RUNNING );
Log( "Resuming from paused state." );
next;
}
elsif( SERVICE_STOP_PENDING == $State )
{
$LastState = SERVICE_STOPPED;
Win32::Daemon::State( [ state => SERVICE_STOPPED, error => 1234 ] );
Log( "Stopping service." );
next;
}
else
{
# Take care of unhandled states by setting the State()
# to whatever the last state was we set...
Win32::Daemon::State( $LastState );
}
Log("Sleeping...");
Win32::Sleep( $SLEEP_TIMEOUT );
}
Log( "Done");
print "StopService() return value: ", Win32::Daemon::StopService(), "\n";
sub Log
{
my( $Message ) = @_;
if( fileno( LOG ) )
{
print LOG "[" . localtime() . "] $Message\n";
}
}
When I run this program it exits immediately with output:
> C:\Users\hakon\perl\service\deamon>perl p.pl
DB_DIR: C:\Users\hakon\perl\service\deamon
DB_FILE_NAME : p.log
StopService() return value: 1
The output of the log file p.log
is:
> C:\Users\hakon\perl\service\deamon>type p.log
# Service Starting
# Script: p.pl
# PID: 8900
# Date: Tue Jun 30 20:01:46 2020
[Tue Jun 30 20:01:46 2020] Starting service
[Tue Jun 30 20:01:46 2020] Entering service loop
[Tue Jun 30 20:01:46 2020] Done
What am I missing here? Why am I getting SERVICE_STOPPED
immediately before entering the while
loop?
See also this question on stackoverflow.com
Metadata
Metadata
Assignees
Labels
No labels