I'm poking around the bits and pieces of David Roths Win32::Daemon and attempting to learn how to use it to create and install services on Win32 machines. So, using some code from Tim Roth's
Win32 Perl Scripting book, I've created the two small scripts below which I am attempting to get working correctly so I can modify it a piece at a time, and learn what all I can do with Win32::Daemon. The code is as follows:
use Win32::Daemon;
open ( OUT, "C:\\Out.txt" ) || die ( "Unable to open file: $!" );
Win32::Daemon::StartService();
while ( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) )
{
if ( SERVICE_START_PENDING == $State )
{
print OUT "Starting Service\n";
Win32::Daemon::State( SERVICE_RUNNING );
}
elsif ( SERVICE_PAUSE_PENDING == $State )
{
print OUT "Pausing Service\n";
Win32::Daemon::State( SERVICE_PAUSED );
}
elsif ( SERVICE_CONTINUE_PENDING == $State )
{
print OUT "Resuming Service\n";
Win32::Daemon::State( SERVICE_RUNNING );
}
elsif ( SERVICE_STOP_PENDING == $State )
{
print OUT "Service Stopping";
Win32::Daemon::State( SERVICE_STOPPED );
}
elsif ( SERVICE_RUNNING == $State )
{
print OUT "Service Running";
}
sleep(5);
}
Win32::Daemon::StopService();
I install the daemon with this:
#!/usr/bin/perl -w
use Win32::Daemon;
my %ServiceConfig = (
name => "TestService",
display => "Testing a Perl Created Service",
path => $^X,
user => '',
passwd => '',
parameters => 'c:\Perl\scripts\TestService.pl',
);
if ( Win32::Daemon::CreateService ( \%ServiceConfig ) )
{
print "The '$ServiceConfig{display}' service";
print "was successfully installed.\n";
}
else
{
print "Failed to add '$ServiceConfig{dislay}' service\n";
print "Error:";
print Win32::FormatMessage( Win32::Daemon::GetLastError() ), "\n";
}
And it installs happily. But when I open up Services, and choose "Start" I
get:
The service did not respond to the start or control request in a timely fashion
Any ideas?
Macphisto the I.T. Ninja
Everyone has their demons....