I hate it when ... invalid models

| No Comments
I hate it when my internal model of a program doesn't quite match its actual behavior.

So I'm tailing a file: "tail -f file".  Tail recognizes when a file's been truncated and starts rereading it.  BUT, even if you say "-n +0" it still starts rereading from the new end, not from the beginning.  So if you have

# Terminal 1
date > file
tail -f -n +0 file

# Terminal 2
date > file
date > file
date > file
You'll never see anything new from tail, because the length of the file never actually changes.

More subtly, say you have

# Terminal 1
date > file
date >> file
tail -f -n +0 file

# Terminal 2
date > file
Here the tail will see that you truncated the file, but it won't show you the new line, because the file length changed from 58 to 29, not from 58 to 0 to 29.

All that's fine and maybe even "obvious" when laid out like that.  But you can get the same behavior like this:

# Program 1, pseudo-Perl
open LOGFILE, ">file";
while (my $request = get_request()) {
    print LOGFILE "the request\n";
    process( $_ );
}

# Terminal 1
tail -f -n +0 file
When you restart Program 1, you won't see the first request in the log file, because just as above, the file length went from (say) 100 to 50, not 100 to 0 to 50.

But if you cat the file, there it is.

Anyway, I hate it when that happens.

(The answer, by the way, is to unlink the file before you start writing to it again, and tell tail "--follow=name".)

Leave a comment

About this Entry

This page contains a single entry by Larry Clapp published on February 18, 2010 9:17 AM.

An interesting practical tutorial on states of alertness. was the previous entry in this blog.

xkcd: Devotion to Duty is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.