turnstep's reply made me realize that the second half of the above proposal isn't portable. Recording the file size doesn't do you that much good except on Unix. If you start a script a while later and you want to read what lines were added since the last time that the script ran, then you need to know the seek offset to the previous end of the file; and this isn't the file size except on Unix (at least is isn't for DOS, Win32, OS/2, MacOS, nor VMS).

Another problem is to detect when the file has been replaced and you need to start from the beginning again. Checking for a changed i-node number (from stat) works pretty good, even on many non-Unix systems. But even under Unix, it misses some cases (and gives a false positive on other rare cases). Just noticing that the file got smaller is often an acceptable method. You can also cache the first few and/or last few lines and compare those quite quickly (or just remember the seek offset right before the last newline and check that the character at that offset is still a newline). Or combine all of these and reread the whole file if any of them fail, for example.

This type of approach can be quite nice if you don't need the lines processed immediately. It doesn't require one more process always hanging around. It is easy to "catch up" if something goes wrong.

        - tye (but my friends call me "Tye")

In reply to (tye)Re: checking file everytime a new line is added to file by tye
in thread checking file everytime a new line is added to file by Anonymous Monk

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.