Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to force tail -f continuous

Status
Not open for further replies.

suhaimi

Technical User
Aug 3, 2001
71
US
Hi all,
How do I continuously tail a modified file?. Please read this carefully. I know "tail -f" will do the trick. But tail -f will work half way if we move the file to different name and work on the same file with fresh data.
example:
---------------------------
-- test.sh
x=0
while [ $x -eq 0 ]
do
touch test.log
echo "Mydate: `date`" >> test.log
sleep 10
done
----------------------------------
>./test.sh
>tail -f test.log
It will tail the file continuously, but if we move the log, tail -f will no longer tail.
example:
> mv test.log test.bkp
Running "tail -f test.log" will hang/not tail any longer.

How do I make the tail process continuous even we move the file?

Rgds,
Suhaimi



 
The short answer is: don't mv the file. Instead use...

cp test.log test.bkp; >test.log
 
Hi,
The option you mentioned is not feasible since I have no control over the process. It's a part of system application (in .exe file). There is no way I could tap into the system and change mv to cp. Nice try though. Anybody?????

Suhaimi
 
create a procedure that tail -f's, and kill the process off, and restart after the file is moved ... if you know when it is moved.
 
It is hard to determine when the file will be moved. There are certain patterns that trigger the move. I think it'll move the file whenever there is error. Moreover, there are thousands of special error codes that count.
 
To quote from the manual page for GNU tail
Code:
`--max-unchanged-stats=N'
     When tailing a file by name, if there have been N (default
     n=5) consecutive iterations for which the size has remained the
     same, then `open'/`fstat' the file to determine if that file name
     is still associated with the same device/inode-number pair as
     before.  When following a log file that is rotated, this is
     approximately the number of seconds between when tail prints the
     last pre-rotation lines and when it prints the lines that have
     accumulated in the new log file.  This option is meaningful only
     when following by name.
This option is designed specifically for the problem of having the underlying file move from underneath you.
 
Maybe could you use the "watch -n NumberOfSeconds tail -f YOURFILE" command for doing what you want...

That's it if I've understand your meaning.

Alex
 
Is there a "watch" command in unix? I couldn't find any "watch" in my Unix box.
 
Use Salems answer if you have GNU tar.

If not, you need to script your own solution. You dont describe how or why the file is moved, so assuming its a rotated log file, write a script 'mytail' that does something like the following:

(1) Start a background sub-process tail command sending all output to stdout, and remember the PID of this process. I use ksh for sub-processes.

(2) Every N seconds, wake up and get the current size of the file. If it is smaller than the last time you woke up, kill the current tail process and restart it. Otherwise, remember the size.

I've used a number of methods to achieve this, and although its not 100% reliable, this method is simple and has worked for me in the majority of cases.

Other methods I've used are:

- comparing inodes
- implementing the GNU method in C (follow the inode)

 
Suhaimi

I've no unix box there so I can't verify, but I'm sure there is one on Linux... That's why I said that

Alex
 
Thanks alot guys, I've asked my admin to load GNU tail. It works fine.

Rgds,
Suhaimi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top