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.


In reply to Win32::Daemon + IPC::Run redirect problem by chris212

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.