Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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?

In reply to Re: Tail'ing a log that frequently rolls over by hbm
in thread Tail'ing a log that frequently rolls over by frd1963

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (13)
    As of 2018-06-19 16:19 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (114 votes). Check out past polls.