in reply to Tail'ing a log that frequently rolls over
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 (Patriarch) on Jan 15, 2009 at 18:08 UTC | |
by hbm (Hermit) on Jan 15, 2009 at 18:43 UTC |