Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: kill a child / parent process (Service Skeleton)

by AcidHawk (Vicar)
on Apr 10, 2003 at 18:36 UTC ( [id://249705]=note: print w/replies, xml ) Need Help??


in reply to kill a child / parent process

I mentioned that I use Win32::Daemon to create and control my Win32 Service. I got the following skeleton from the Roth.net web site.

#!perl.exe ###################################################################### +################## ## ## Test Service v 1.2 - Script to test ser +vice ## ###################################################################### +################## 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 install +ed.\n"; } else { print "Failed to Install $service_config->{display} service.\n +Error: " . 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 <Path>] [-t <Time>] [-remove][-install [-user <User> -pass + <Pwd}]] -t <Time>.......Time in seconds to wait per check. This is the mai +n loop sleep time so a long value will slow service updat +es. -l <Path>.......Path to store the log file. Make this an empty str +ing ("") to disable logging (making this service usele +ss). Default: $Config{log_file} -user <User>....User account the service is to run under. -pass <Pwd>.....The user account's password. -install........Installs this script as a Win32 service. Any additional parameters passed in will set be wh +at the script uses when running as a service. -remove.........Removes this script as a Win32 service. Examples +: perl $0 -install perl $0 -install -user Domain\\User -pass UserPassword -l c:\\moni +tor.log -d c:\\uploads perl $0 -remove perl $0 -l "c:\\winnt\\system32\\logfiles\\monitor.log" -t 120 EOT } #Display Errors sub GetError { return( Win32::FormatMessage( Win32::Daemon::GetLastError() ) ); }

-----
Of all the things I've lost in my life, its my mind I miss the most.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://249705]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-03-29 12:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found