chris212 has asked for the wisdom of the Perl Monks concerning the following question:
When I run my script from a command-line, the output is redirected to a file like it should. When it runs as a Windows service, it does not. When "install" is passed as an argument, it will create a service as the same name as the script file. The output should be redirected to a log file in the same name as the script (.log instead of .pl) in the same directory.
use strict; use IPC::Run ('run'); 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/\.[^\.]*$//; 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 "Run ping...\n"; print LOG localtime()."\n"; run(['ping','-n','5','127.0.0.1'],'>&',\*LOG); my $ec = $? >> 8; print LOG "Exit Code $ec\n\n"; close(LOG); print "Exit Code $ec\n"; Win32::Daemon::StopService();
Is this a IPC::Run bug, a Win32::Daemon bug, or am I doing something wrong? Thanks.
UPDATE:
I found that this bug only seems to affect the process start as a service. Child processes started by that process will work correctly. Also, if you close STDOUT, you will see the same behavior from the command line.
UPDATE 2:
Since child processes are not affected, I was going to ignore this bug. However, since Win32::Daemon can no longer handle shutdowns properly, that kind of defeats the purpose of converting it to a service, so all of this effort has been wasted.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Win32::Daemon + IPC::Run redirect problem
by Marshall (Canon) on May 05, 2017 at 23:12 UTC | |
by chris212 (Scribe) on May 08, 2017 at 20:29 UTC | |
|
Re: Win32::Daemon + IPC::Run redirect problem
by 1nickt (Canon) on May 05, 2017 at 19:41 UTC | |
by chris212 (Scribe) on May 05, 2017 at 20:35 UTC | |
by Marshall (Canon) on May 07, 2017 at 18:53 UTC | |
by chris212 (Scribe) on May 08, 2017 at 20:24 UTC | |
by huck (Prior) on May 08, 2017 at 20:35 UTC | |
by chris212 (Scribe) on May 08, 2017 at 22:06 UTC |