C:\Projects_Perl\testing>perl serva.pl install
service name: serva
Log file: C:\Projects_Perl\testing\serva.log
Successfully added.
####
C:\Projects_Perl\testing>net start serva
The serva service is starting.
The serva service could not be started.
The service did not report an error.
More help is available by typing NET HELPMSG 3534.
C:\Projects_Perl\testing>type serva.log
Fri May 5 15:57:50 2017
Exit Code 0
Run this service...
Fri May 5 15:59:22 2017
Exit Code 0
####
use strict;
use IPC::Run3; # Exports run3() by default
use Win32::Daemon;
use Cwd 'abs_path';
my $script = abs_path($0);
my $logfile = $script;
$logfile =~ s/\.[^\.\\\/]*$/.log/;
my $svcname = $script;
$svcname =~ s/^.*[\/\\]//g;
$svcname =~ s/\.[^\.]*$//;
print "service name: $svcname\n";
print "Log file: $logfile\n";
if($ARGV[0] eq 'install') {
my %service_info = (
machine => '',
name => $svcname,
display => $svcname,
path => $^X,
user => '',
pwd => '',
description => 'test case',
parameters => $script
);
if( Win32::Daemon::CreateService( \%service_info ) )
{
print "Successfully added.\n";
}
else
{
print "Failed to add service: " . Win32::FormatMessage( Win32::Daemon::GetLastError() ) . "\n";
}
exit;
}
Win32::Daemon::StartService();
open(LOG,'>>',$logfile) or die("Failed to write to logfile\n$!\n");
Win32::Daemon::State( Win32::Daemon->SERVICE_RUNNING );
print LOG "Running this service...\n";
print LOG localtime()."\n";
#run3(['ping.exe', '-n', '5', '127.0.0.1'],\undef,\*LOG);
my @result = `ping -n 5 127.0.0.1`; ########### changed
print LOG @result; ########### changed
my $ec = $? >> 8;
print LOG "Exit Code $ec\n\n";
close(LOG);
print "Exit Code $ec\n";
Win32::Daemon::StopService();