#!perl.exe ######################################################################################## ## ## Test Service v 1.2 - Script to test service ## ######################################################################################## use Getopt::Long; use Win32; use Win32::Daemon; my %Config = (timeout_value => 2, log_file => join( "", Win32::GetFullPathName( $0 ) ), ); $Config{log_file} =~ s/[^.]*?$/log/; Getopt::Long::Configure( "prefix_pattern=(-|\/)" ); $Result = GetOptions( \%Config, qw( install|i remove|r timeout_value|t=i log_file|l=s account_id|user=s account_password|pass=s help|?|h ) ); $Config{help} = 1 if( ! $Result || scalar @ARGV ); if( $Config{install}) { &Install(); exit(); } elsif( $Config{remove}) { &Remove(); exit(); } elsif( $Config{help}) { &Syntax(); exit(); } #Open Log File if( open( LOG, ">>$Config{log_file}" ) ) { my $TempSelect = select( LOG ); $| = 1; select( $TempSelect ); print LOG "# Date: " . localtime() . "\n=================\n"; } if( ! Win32::Daemon::StartService()) { if( fileno( LOG ) ) { print LOG "Failed to start this script as a Win32 service.\n"; print LOG "Error: " . GetError() . "\n"; close( LOG ); } exit(); } $PrevState = SERVICE_STARTING; $cnt=0; while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) ) { if( SERVICE_START_PENDING == $State ) { # Initialization code Win32::Daemon::State( SERVICE_RUNNING ); $PrevState = SERVICE_RUNNING; } elsif( SERVICE_PAUSE_PENDING == $State ) { # "Pausing..."; Win32::Daemon::State( SERVICE_PAUSED ); print LOG "Service is Paused \n"; $PrevState = SERVICE_PAUSED; next; } elsif( SERVICE_CONTINUE_PENDING == $State ) { # "Resuming..."; Win32::Daemon::State( SERVICE_RUNNING ); print LOG "Service Continue\n"; $PrevState = SERVICE_RUNNING; next; } elsif( SERVICE_STOP_PENDING == $State ) { # "Stopping..."; Win32::Daemon::State( SERVICE_STOPPED ); $PrevState = SERVICE_STOPPED; next; } elsif( SERVICE_RUNNING == $State ) { # The service is running as normal... $cnt++; print LOG "Sending $cnt \n"; sleep 10; $PrevState = SERVICE_RUNNING; } else { # We have some unknown state... # reset it back to what we last knew the state to be... Win32::Daemon::State( $PrevState ); sleep( $Config{timeout_value} ); } } #Stop the Service Win32::Daemon::StopService(); if( fileno( LOG ) ) { print LOG "================================\n"; print LOG "Service Stopped.\n " . localtime() . "\n"; close( LOG ); } ######################################################################################## ## ## SUB ROUTINES ## ######################################################################################## #Configuration for Service sub GetServiceConfig { my $script_path = join("",Win32::GetFullPathName($0)); my %hash = ( name => 'X-TmpSvc', display => 'X-TmpSvc', path => $^X, user => $config{account_id}, password => $config{account_password}, parameters => "$script_path -l \"$Config{log_file}\" -t \"$Config{timeout_value}\"", ); return(\%hash); } #Install Service sub Install { my $service_config = GetServiceConfig(); if (Win32::Daemon::CreateService($service_config)) { print "The $service_config->{display} was Successfully installed.\n"; } else { print "Failed to Install $service_config->{display} service.\nError: " . GetError() . "\n"; } } #Remove Service sub Remove { my $service_config = GetServiceConfig(); if( Win32::Daemon::DeleteService( $service_config->{name} ) ) { print "The $service_config->{display} was successfully removed.\n"; } else { print "Failed to remove the $service_config->{display} service.\nError: " . GetError() . "\n"; } } #Display if no Command Line Arguments Found! sub Syntax { print << "EOT"; A Simple Win32 service Syntax: [-l ] [-t