[CLUE-Tech] Tail -f + grep

Jeffery C. Cann jccann at home.com
Thu Feb 1 06:25:00 MST 2001


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thursday 01 February 2001 00:14, Craig Magtutu wrote:

>   one unfortunate feature of tail is that when the file is deleted or
> replaced, the real-time tail'ing appears to stop.

This is because the file handle that tail was reading from is no longer valid 
if a file is replaced or deleted.  A file handle is simply an address that 
the operating system uses to interact with a file.  By definition a file 
handle is invalid if the file does not exist.  (see below for an alternative)

The tail command cannot read a new file handle without restarting the 
command.  This is a side effect of using file handles at the filesystem 
level.  AFAIK, all file systems have this problem, not just ext2 (Linux 
default file system).

Not sure how to get around this unless the design and implementation of file 
systems changes significantly.

Another way to do the same thing and not worry about restarting tail is to 
use a shell command while loop:

$ while [ -e /var/log/messages ]
> do
> grep REJECT /var/log/messages
> sleep1
> done

This loop will do the following:

while the /var/log/messages file exists (-e test)
grep the messages file
wait for a second
repeat the loop.

If the messages file is replaced, chances are that it will not happen when 
the -e test executes, so when cron cycles your file, it won't stop executing 
like it does in test.  The other advantage of the while loop over tail is 
that it is not constantly grep'ing your messages file because the sleep 
command stops execution for 1 second in my example.  However, you can change 
it to 'sleep 3600' if you want it to execute once an hour.  But, you may as 
well use cron to schedule a job that occurs once an hour so you can be sure 
the system executes it.

Later
Jeff

- -- 
jccann [at] home [dot] com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.4 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iEYEARECAAYFAjp5Y7AACgkQw3/GBQk72kA1bwCdHaGRa/Z7taAYiFaPoHIZI2sp
UV0An2yi75JF/YQUfB4GEZi8XmMgljBF
=9c36
-----END PGP SIGNATURE-----



More information about the clue-tech mailing list