Unix questions - Tailing rotating log files?

This is Interesting: Free IT Magazines  
Home > Archive > Unix questions > December 2004 > Tailing rotating log files?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Tailing rotating log files?
Roy Smith

2004-12-16, 7:44 pm

I've got a program which write a log to foo.log, and I want to watch
the contents of that file; tail -f would work fine, but for one
hitch. Each time the program restarts, it closes the log file, moves
the old log to foo.log.bak, and starts a new foo.log.

I know some tails have a -F which does exactly that, but I'm on
solaris, and the solaris tail doesn't support -F. Is there a simple
way to fake this out on solaris?
Bill Marcum

2004-12-16, 7:44 pm

On 14 Dec 2004 11:14:19 -0500, Roy Smith
<roy@panix.com> wrote:
> I've got a program which write a log to foo.log, and I want to watch
> the contents of that file; tail -f would work fine, but for one
> hitch. Each time the program restarts, it closes the log file, moves
> the old log to foo.log.bak, and starts a new foo.log.
>
> I know some tails have a -F which does exactly that, but I'm on
> solaris, and the solaris tail doesn't support -F. Is there a simple
> way to fake this out on solaris?


Use a crontab to check the size, mtime, and/or inode number of foo.log;
have it restart your script if the inode changes or the size decreases.


--
cowsay -b "Prepare to be assimoolated! "
Ed Morton

2004-12-16, 7:44 pm



Roy Smith wrote:
> I've got a program which write a log to foo.log, and I want to watch
> the contents of that file; tail -f would work fine, but for one
> hitch. Each time the program restarts, it closes the log file, moves
> the old log to foo.log.bak, and starts a new foo.log.
>
> I know some tails have a -F which does exactly that, but I'm on
> solaris, and the solaris tail doesn't support -F. Is there a simple
> way to fake this out on solaris?


You could do something like this (untested):

_file="$1"
_start=0
while :
do
_end=`wc -l < "$_file"`
_end="${_end##* }"
if (( $_end < $_start ))
then
# the file has been moved to a backup
_tailFile="${_file}.bak"
_end=`wc -l < "$_tailFile"`
_end="${_end##* }"
else
_tailFile="$_file"
fi
if (( $_end > $_start ))
then
_start=$(( $_start + 1 ))
sed -n "${_start},${_end}p" "$_tailFile"
fi
if [ "$_tailFile" = "$_file" ]
then
_start="$_end"
else
_start=0
fi
sleep 1
done

I didn't spend very long thinking about the above (which I tweaked from
a different script) but hopefully you get the idea - just use sed to
print out the number of lines added to the file (or it's backup if just
created) in about the past second.

Regards,

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com