Operating System - HP-UX
1827315 Members
5819 Online
109961 Solutions
New Discussion

tail on deleted/re-recreated file

 
SOLVED
Go to solution
Zigor Buruaga
Esteemed Contributor

tail on deleted/re-recreated file

Hi,

First, my OS's are HP-UX 10.20 & 11.0
When I use "tail" command to monitor a log file, if that file is deleted and recreated with the same name ( while tail is running ), then "tail" still runs but never show new lines added to the file. As far as I know, that is the normal behaviour.
Anybody knows if exists some sort of utility/command that can "monitor" a file even if it's deleted and recreated with the same name?

Thanks in advance.
Kind regards,
Zigor
8 REPLIES 8
Mark Grant
Honored Contributor

Re: tail on deleted/re-recreated file

As names don't mean too much to unix, I don't think you are going to succeed here. When you do a "tail -f" on a file and the file is deleted, the file doesn't really go away until you stop doing your "tail". Therefore, if you create a new file, even with the same name, it will be a different file.The only way this is going to work is if you script tail to set an alarm every now and then to stop "tailing" and start again.
Never preceed any demonstration with anything more predictive than "watch this"
David_246
Trusted Contributor

Re: tail on deleted/re-recreated file

Hi Zigor,

As explained a file is not a file, but an inode. If you want to empty the file to be able to start all over, it's better to null it then to remove and re-create it. Else there is no "tail" solution possible. Of course Perl or other scripts can be written to work around it.

>/path/to/my/file

is an easy way to null a file, the inode remains then so that also tail -f will start working.

Regs David

B.T.W. "cp" will create a new inode where a "mv" on the same FS will keep the inode.
@yourservice
john korterman
Honored Contributor

Re: tail on deleted/re-recreated file


Hi Zigor,

I have just tried a workaround, which unfortunately did not work! First, I had a continous tail running on a log file, e.g.:
# tail -fn1 your_log_file
I then appended to that file, e.g.:
# echo "helli" >> your_log_file
# echo "hello" >> your_log_file
to which the continous tail reacted.
I then tried to set the file pointer of your_log_file into zero position:
# cat /dev/null > your_log_file
which truncates the file to zero bytes. Unfortunately the tail command is not aware of this; it still looks at the position it was on before the /dev/null action. Therefore, it is not until you write past the position before the "dev/null" action that the tail command reacts again.
I cannot see how to overcome this, except by creating the log file as a fifo, e.g.:
# mkfifo your_log_file
# tail -1fn your_log_file
# echo "halli hallo" > your_log_file

which of course is not what you want, as it raises a number of other problems, but I am looking forward to the coming suggestions!


regards,
John K.
it would be nice if you always got a second chance
Zigor Buruaga
Esteemed Contributor

Re: tail on deleted/re-recreated file

Hi,

Thanks for the replies.
Forgot to mention that I have not control over the log files, so I can not create, copy or move them.
I think I need some kind of new "tail" binary, that can do some kind of monitoring over the original inode and reopen the original file name in case it has been re-created ... or something like that ;-)

Thanks again for all your replies.
Kind regards,
Zigor

Mark Grant
Honored Contributor

Re: tail on deleted/re-recreated file

Zigor,You will not be able to do that either.When you the log file gets re-created, it will have a new inode even if it has the same name. There is no way around this.As far as I can see, your only possibility is to "tail -f" and then interupt it and start again. You might miss some lines though. You can interupt it if you use a script which starts a sleep process in the background which also send the main script a signal say SIGUSR1 when the sleep has finished. The main script traps this signal and starts the tail again.
Never preceed any demonstration with anything more predictive than "watch this"
Zigor Buruaga
Esteemed Contributor

Re: tail on deleted/re-recreated file

Hi,

Now it's clear, I'm looking for something impossible, ... or not developed yet ( it could be a good and very useful improvement ).
Anyway, thanks to all.

Kind regards,
Zigor
Martin Robinson_4
New Member
Solution

Re: tail on deleted/re-recreated file

Although this entry is old, this reply mnay still be of some use. I had the same problem and used Google.

There is a module for Python call TailFollow which addresses this exact problem. Unfortunately HPUX ships with Python 1.something; TailFollow expects 2.0 or later. I was able to downgrade the core component of TailFollow to 1.something capabilities.
Kent Ostby
Honored Contributor

Re: tail on deleted/re-recreated file

Zigor --

Another option would be to periodically (once a minute or whatever), make a copy of the log file and diff it to your previous copy and then have your script react that diff instead.

Best regards,

Kent M. Ostby
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"