Re: [Hampshire] tailing a file as/after it gets rotated?

Top Page

Reply to this message
Author: Steve Wesemeyer
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] tailing a file as/after it gets rotated?
On Friday 06 February 2009, Victor Churchill wrote:
> Hi,
>
> I have a log file on a server, which I like to keep an eye on by
> running tail -f logfile (in fact I then pipe that through other stuff
> too). This sits in a dedicated screen session.
>
> Trouble is the file I am tailing gets moved by a cron job at 06:00
> every day and replaced by a new empty one. My tail job keeps watching
> the old log file even though it has now been renamed by the rotate
> procedure, and the new logfile is untailed.
>
> I am sure this is not a new thing but a (not so) quick google did not
> come up with a way round this. All I can think of is to have the cron
> job that rotates the log file send a HUP signal to any process tailing
> it, but I'm not sure how one would go about that or if that would
> actually work. (Having just experimented, I suspect not.)
> ( I have just noticed that this paragraph includes five occurrences of
> the word 'not', and that's not including these two. ;-)
>
> SO, does anybody know of a way to keep a tail on-target when the files
> get whiked away and replaced?


from the tail man page

With --follow (-f), tail defaults to  following  the  file  descriptor,
       which  means that even if a tail'ed file is renamed, tail will continue
       to track its end.  This default behavior  is  not  desirable  when  you
       really want to track the actual name of the file, not the file descrip-
       tor (e.g., log rotation).  Use --follow=name in that case.  That causes
       tail  to track the named file by reopening it periodically to see if it
       has been removed and recreated by some other program.


Cheers, 
    Steve