Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

mhearse,

If you are doing this once, then reading the whole file is fine, but if every 'nn' minutes you want to run the script then saving the location of the end-of-file would be a good solution. This technique is similar to 'pop-before-smtp' technique. (Example, not tested )

my $File_Loc = 0; my $SaveLocFile = "./FileLoc"; if ( -e $SaveLocFile ) { open( my $IN, "<", $SaveLocFile ) or die "File Bad $!"; $File_Loc = < $IN >; chomp ( $File_Loc ); close $IN; } my $sz = ( -s $SaveLocFile ); if ($sz > 0) { open( my $log, "<", $log_file"); ## This should be log f +ile if ($sz < $File_Loc ) { $File_Loc = 0; } ## New log file seek $log, $File_Loc, 0; ## Move to old end of f +ile while ( < $log > ) { chomp; my $line = $_; # do work } $File_Loc = tell $log; ## Save current end-of-file location open( my $OUT,">", $SaveLocFile ); print $OUT "$File_Loc\n"; close $OUT; close $log; }

Since you will be running on a log server, you should use the same locking mechanism as the log daemon. There are some race conditions in the code, but if you save where you ended, you shouldn't miss anything. I originally wrote this in 1997, so I tried to update the technique. Looking at the original code, it isn't the way I would write it today, but it has worked on some 40 - 50 mail servers since, and I never had to rewrite it (probably should now).

Hopefully you see the idea and can adapt this to your situation.

Good Luck

"Well done is better than well said." - Benjamin Franklin


In reply to Re: Using binary search to get the last 15 minutes of httpd access log by flexvault
in thread Using binary search to get the last 15 minutes of httpd access log by mhearse

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2014-08-02 04:48 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Who would be the most fun to work for?















      Results (54 votes), past polls