Hi echoangel911,

I have to slightly disagree with both of the answers above.

If the process you're trying to read output from is under your control (eg. a Perl script), then Grandfather's advice will work, for the program doing the output.

However, if you are getting the output from some other program which you don't have the source code to, you may be out of luck.  This is because the system is probably buffering the output; saving up the output until a certain fixed amount is reached, after which it will send the entire buffer.  The only way the process will send the a small amount of data is when the process finishes, and it closes its output file, which will cause the buffer to flush.

Unless you have control over the flushing mechanism of the program producing the output, you'll just have to be patient until the process finishes.

Here's an example.  Let's say that you *do* have the source to the first program (the one producing the output), and it's call proc1.pl:

#!/usr/bin/perl -w use strict; use warnings; for (my $i = 1; $i <= 60; $i++) { sleep 1; print "$i\n"; }

Now, here's hgolden's program (modified slightly to avoid global variables, and to die if the pipe open failed).  The second program, proc2.pl looks like this:

#!/usr/bin/perl -w use strict; use warnings; my $cmd = "proc1.pl"; open(my $pipefh, "$cmd |") or die "Unable to pipe to command '$cmd' ($ +!)\n"; my $out; while ($out = <$pipefh>) { print $out; }

You will likely see, when you run proc2.pl, that no output is produced until proc1.pl finishes, which will take 60 seconds.  That's because the output is buffered until a certain amount is written.  The way to fix it is by modifying proc1.pl to set autoflush true as Grandfather described:

#!/usr/bin/perl -w use strict; use warnings; $| = 1; for (my $i = 1; $i <= 60; $i++) { sleep 1; print "$i\n"; }

Now, you should see output from proc1.pl every second.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

In reply to Re: real time output by liverpole
in thread real time output by echoangel911

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.