Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Tail'ing a log that frequently rolls over

by hbm (Hermit)
on Jan 15, 2009 at 16:54 UTC ( #736586=note: print w/replies, xml ) Need Help??


in reply to Tail'ing a log that frequently rolls over

I've been tail'ing this thread with great interest! I currently monitor a log with this daemon. Notable differences from OP's environment - I'm on Unix and I rotate the log myself, monthly.
use strict; use warnings; use POSIX; use IO::Handle; my $appLog = '/tmp/t.txt'; my $error = 'Fatal Error'; exit if (my $pid = fork); defined($pid) or die "Can't fork: $!"; POSIX:setsid() or die "Can't setsid: $!"; $SIG{TERM} = sub { close LOG; exit; }; $SIG{INT} = $SIG{TERM}; $SIG{PIPE} = 'IGNORE'; $SIG{HUP} = \&readLog; sub readLog { open(LOG,"<$appLog") or die("Unable to open $appLog: $!"); while (<LOG>) { ;; } # read to the end of the log LOG->clearerr(); # and clear EOF flag } &readLog; for (;;) { while (<LOG>) { #&pageMe if /$error/; print; } sleep 2; LOG->clearerr(); # clear EOF flag }

This works pretty well for me. On the downside, it quietly stops working if the log is truncated or created anew. But I work around that by HUP'ing it after I rotate the log.

But hello, -F! (Who knew about it? Not my, um, man tail.) So I've tried this:

use strict; use warnings; use POSIX; my $appLog = '/tmp/t.txt'; my $error = 'Fatal Error'; exit if (my $pid = fork); defined($pid) or die "Can't fork: $!"; POSIX:setsid() or die "Can't setsid: $!"; my $tail = open(LOG,"/usr/local/bin/tail -F $appLog|") or die "Ca +n't tail: $!"; $SIG{INT} = sub { close LOG; kill 3, $tail; exit; }; $SIG{TERM} = $SIG{INT}; $SIG{PIPE} = 'IGNORE'; $SIG{HUP} = 'IGNORE'; while (<LOG>) { #&pageMe if /$error/; print; }

This seems a bit more succinct; and it gracefully handles a truncated or recreated file. A downside, perhaps, is that it creates a second process; but more importantly if I kill the daemon, the tail process doesn't receive the signal until the log is next written-to.

Any thoughts on getting my original daemon to internally handle the truncated/recreated log?

Or how the second daemon can kill the tail -F immediately?

Replies are listed 'Best First'.
Re^2: Tail'ing a log that frequently rolls over
by BrowserUk (Pope) on Jan 15, 2009 at 18:08 UTC

    You could try this:

    use strict; use warnings; use POSIX; use Perl::Unsafe::Signals; my $appLog = '/tmp/t.txt'; my $error = 'Fatal Error'; exit if (my $pid = fork); defined($pid) or die "Can't fork: $!"; POSIX:setsid() or die "Can't setsid: $!"; my $tail = open(LOG,"/usr/local/bin/tail -F $appLog|") or die "Can't tail: $!"; $SIG{INT} = sub { close LOG; kill 3, $tail; exit; }; $SIG{TERM} = $SIG{INT}; $SIG{PIPE} = 'IGNORE'; $SIG{HUP} = 'IGNORE'; UNSAFE_SIGNALS { while (<LOG>) { #&pageMe if /$error/; print; } };

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Some day, but I'm stalled at Perl 5.8.0 and it requires 5.8.1. Thanks for all your suggestions on this topic!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://736586]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2018-07-18 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (383 votes). Check out past polls.

    Notices?