I've been giving this problem some thought and I think a technique from the O'Reilly Perl Cookbook might be applicable. Instead of shelling out to /usr/bin/grep you could open file-B for reading inside your script. You could then read the file line by line in a while ( <$filehandle> ) { ... } loop until end of file, using Perl's grep or, more likely, simple pattern matching to select lines to print to file-A. Once EOF has been reached you could then sleep for 20 minutes, without closing the filehandle, before using seek on awakening to reset the error condition and continue reading exactly where you left off.

The file rotation throws a few wrinkles into things but they should not be insurmountable. If the file is rotated by renaming the old file (e.g. to file-B.1 etc.) and creating a new file-B the script will still have the original file open and can read the remaining lines before close'ing that and open'ing and processing the new log file. This could be detected by doing a stat on the file when opening it and cacheing the device and inode, checking whether the current file-B's device and inode has changed. If the file is not rotated but is just truncated instead then you could detect this by doing a tell on the filehandle before sleeping and checking that the log file on awakening is now smaller than previously. That assumes that the file doesn't hit the 10MB limit in the 20 minute time frame.

All of the above would be within an endless while ( 1 ) { ... } loop so you would install signal handlers for INT, TERM and QUIT signals so that the script can terminate tidily.

I hope these thoughts are of interest and are helpful.

Cheers,

JohnGG


In reply to Re: extract the tail from a string (with new lines) containing a substring by johngg
in thread extract the tail from a string (with new lines) containing a substring by jjmoka

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.