Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^3: Tail'ing a log that frequently rolls over

by BrowserUk (Pope)
on Jan 12, 2009 at 21:14 UTC ( #735809=note: print w/replies, xml ) Need Help??


in reply to Re^2: Tail'ing a log that frequently rolls over
in thread Tail'ing a log that frequently rolls over

any form of the "follow" options (e.g., "tail -f", "tail --follow", etc.) it never gives anything

Hm. It's possible you have a newer version that is fundementally broken and nobody has noticed, but I seriously doubt it. It is more likely to be how you are using it. The following two scripts demonstrate that it works:

A simulated high speed, rotating logger:

#! perl -slw use strict; my $logname = 'theLog'; while( 1 ) { open LOG, '>', $logname or die $!; for( 1 .. 1000 ) { print LOG "Line $_ of the log with some superfluous extra text + as filler"; } close LOG; my $n = 0; $n++ while -e "$logname.$n"; rename $logname, "$logname.$n"; }

A tail -F script:

#! perl -sw use strict; my $pid = open LOG, "tail -F theLog |" or die $!; $SIG{ INT } = sub{ close LOG; kill 3, $pid; exit; }; $SIG{ BREAK } = $SIG{ INT }; while( <LOG> ) { print; }
the tail.exe process needs to be maually killed.

Methinks you have unreal expectations. Any version of tail on any platform when used with -F (--follow=name) will have to be explicitly terminated, whether manually or programmically, by some external influence. It could not be any other way.

When following a rotating named file, there will always be a short but real period between the renaming of the old log and the creation of the new one. Sp tail has to be written to ignore any 'file not found' errors from open and retry. There is therefore, no reasonable condition for it to self terminate.

So your script would have to decide the conditions under which tail should be terminated and call kill to do so using the $pid returned by the open. (See the signal handler above.)

So then it falls to your script to decide when it should terminate. Whether because

  • the user interrupted it from the keyboard or via some other action;
  • or because (for example,) it is monitoring the program that is producing the log and detects that it has terminated.

If the latter, it will be necessary for you to detect the demise of the log producer in a timely manner, before re-entering the read state.

You appear to be under the impression that tail will be able to know when the program producing the logs isn't going to create another new one, and self-terminate, but that simply cannot be the case.

Perhaps that is the source of your problems with File::Tail and the POE solutions. Ie. Your expectations are wrong.


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.

Replies are listed 'Best First'.
Re^4: Tail'ing a log that frequently rolls over
by frd1963 (Initiate) on Jan 13, 2009 at 17:53 UTC
    I verified that your scripts fails for me as well. The only change I made was to swap the "tail -F" with "tail -f"

    Can you tell me where you got your tail.exe? Hopefully it is standalone.

    Thanks again, for at least helping me prove my own sanity ;)

      The only change I made was to swap the "tail -F" with "tail -f"

      -f is equivalent to --follow=descriptor, it follows the descriptor or filehandle which won't work for a rotating log file. You need -F or --follow=name for that.

      If you email me, (see my homenode), I'll send you a copy of the executable. But it originated from the UnxUtils website, though it may be a couple of years old.


      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.
        You are correct about the "-f" following the file inode, not the filename, but regardless, tail should not have hung. It should at least have outputed the last 10 lines (by default) and then hung.
        In my own scripts, I did use "--follow=name" and got the same results, and I have since tried that in your script and it made no difference.

        I am trying not to hate windows, but I know on Unix, I would have been able to trace the process (truss or strace) to find out why or at least where it is hanging.

        Anyway, I will contact you for that older version of the Tail.exe which seems not to have this bug (and supports "--follow=name" as "-F" though this is more an annoyance than a bug, but may point to the problematic inconsistencies in the newer version.)

        Thanks once again for your assistance,
        -Frd

Re^4: Tail'ing a log that frequently rolls over
by frd1963 (Initiate) on Jan 13, 2009 at 17:02 UTC
    Thanks for the advice. I will be trying the code you provided to see if I get different results. I do notice that you use the "-F" option whereas the version of the tail.exe that I got from the above named site only accepts that in lower case. So we are obviously not using the same version of the utility, if even the same utility at all. Can you let me know where you got your version?

    Also, I wasn't clear in my expectations of tail. I know it wouldn't have any way of knowing the file was no longer being written to, not would I want it to; but I did expect that when its parent process died (perl) it would die as well. Of course, I had to forcefully kill perl to get it to exit which may be the equivalent of a UNIX kill -9 which would explain why tail never exited. It is easy enough to keep track of the PID in perl and kill it when deemed necessary, so I can put the appropriate code in there, but perl hangs at the read and so can't get to the code where it will kill the PID, unless from another thread or by handling an asycronous signal.

    Thanks again for the ideas and I will let you know what I find,
    -Frd

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://735809]
help
Chatterbox?
marto should develop the 'anti-social network'
[marto]: sign up to make it impossible to be contacted online :P
[Discipulus]: karl: configuring the network is like: nmcli conn add con-name "name name" ifname eth0 type eterneth.... not so different from powersehell longness

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-12-14 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (389 votes). Check out past polls.

    Notices?