G'day aquercus,

Welcome to the monastery.

I don't have perl running on any MSWin OSes; however, here's how I might have tackled this on a *nix OS. There may be sufficient here for you to adapt it to your OS. I've added some notes after the code as well as a sample run.

Here's the code (pm_test_tail_pipe.pl):

#!/usr/bin/env perl use strict; use warnings; use autodie qw{:all}; my $tail_cmd = 'tail -f ./pm_test_file_to_tail'; my @tail_captures; print '*** Start reading from $tail_pipe at ', time, "\n"; my $tail_pid = open my $tail_pipe, '-|', $tail_cmd; { local $SIG{INT} = sub { kill INT => $tail_pid if kill 0 => $tail_pid; waitpid $tail_pid => 0; }; while (<$tail_pipe>) { push @tail_captures, $_; } } print '*** Stop reading from $tail_pipe at ', time, "\n"; # Possibly process data captured, e.g. for demo only $_ = uc for @tail_captures; print '*** Captured via $tail_pipe:', "\n"; print for @tail_captures;

Notes:

Sample run:

File to TailTailing Process

Assume some log file that already has some data

$ cat > pm_test_file_to_tail line 1 line 2 line 3

[terminated with Ctrl-D]

 

External program starts appending data

$ cat >> pm_test_file_to_tail line 4 line 5
 
 

Start tailing the file

$ pm_test_tail_pipe.pl *** Start reading from $tail_pipe at 1371707519

External program appends more data

line 6 line 7
 
 

[terminate tailing with Ctrl-C]

^C*** Stop reading from $tail_pipe at 1371707542 *** Captured via $tail_pipe: LINE 1 LINE 2 LINE 3 LINE 4 LINE 5 LINE 6 LINE 7

External program still appending data

line 8 line 9

[External program terminated with Ctrl-D]

 

Check final contents of tailed file

$ cat pm_test_file_to_tail line 1 line 2 line 3 line 4 line 5 line 6 line 7 line 8 line 9
 

-- Ken


In reply to Re: tail pipe by kcott
in thread tail pipe by aquercus

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.