I am trying to write a simple helper script that will call three external programs multiple times (based on some parsed data). My problem is that Perl's system() call is returning before the command finishes. I have looked at the program I'm calling and it is actually a Perl script that calls additional programs (but this script can NOT be changed). The system() call returns while these programs execute in the background. The called program prints a final status message that I thought I could capture with my script to ensure that the system() call completed. Below is the snippet of the called program (that I can't edit) showing the final status message:
if ($exit_status != 0) {
print("-F-Job completed abnormally.(status = $exit_status)\n");
} else {
print("-I-Job completed successfully.\n");
}
As you can see, this is a simple print statement. After various attempts I am unable to capture this output. Below is my code snippet intended to capture STDOUT and STDERR and print each line. In my final program I will change this code to wait until I see either the success or failure message.
$command = "program_to_run $opt{c} -f test";
# print "$command\n";
open(RUN, "$command 2>&1|");
while (<RUN>) {
print "$_\n";
next;
}
close(RUN);
exit 0;
When I uncomment the print $command line it prints the correct system command. However, the print statements from that system command are not being captured. Am I doing something wrong? Thanks in advance for any tips you can provide.
Greg
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.