
farzin
New User
Nov 14, 2008, 8:27 PM
Post #1 of 1
(1172 views)
|
Incomplete output echo
|
Can't Post
|
|
Hi, I have a problem with Perl echoing the entire output of an external command. Within my Perl script I execute a set of external commands (programs). But first I map Perl's stdout to a file to capture all output. Then when I execute the commands (programs), I print their output which goes into the log file. Below is a simplified version of this script: #!/usr/bin/env perl #---- Set line buffering $| = 1 ; #---- Map output to run.log close( STDOUT ) ; open( STDOUT, ">run.log" ) || die "Unable to write to run.log" ; $oldfh = select STDOUT ; $| = 1 ; select $oldfh ; #---- Set up an executable plus command line options $cmd = "some executable with options" ; #---- Execute the command and get the output open( CMD, "$cmd 2>&1|" ) || die "Unable to execute $cmd" ; #---- Get the stdout of the executable and echo back while( <CMD> ) { print $_ ; } #---- Close the command close( CMD ) ; #---- Make sure the command executed with no errors $? == 0 || die "Error occurred executing: $cmd" ; #---- Exit exit( 0 ) ; I have two problems with the above: 1. Some times when an error occurs in the executable, the log file is missing some of the output of the external program, ie., the entire stdout of the cmd is not echoed to the log file. 2. On 64bit Windows, I intermittently loose the last set of output lines from cmd; even if the command exits with no error. Any ideas on how to fix these related problems? Farzin
|