I have a black-box system that periodically updates a single-line "flag" file. This single-line file takes, over the course of the system's life various different values, corresponding to the states of an FSM.

E.g. like "downloading", "validating", "posting", etc. At any given point in time the value is different so the software must be opening the "flag" file, truncating it, and writing the new single-line value.

How can I maintain a log of the succession of different values? I was thinking something like tail -f but with the ability to record the different values and with a way that's guaranteed not to mess with the file system when the software is updating of those values. A simple tail -f fails with "file truncated" after some time.


1 Answer 1


You probably want tail -F (note that it is capitalised), which will retry opening/reading the file if it fails.

From man tail:

   -f, --follow[={name|descriptor}]
          output appended data as the file grows; -f, --follow, 
          and --follow=descriptor are equivalent

   -F     same as --follow=name --retry

          keep trying to open a file even when it is or becomes
          inaccessible; useful when following by name, i.e., with

If your version of tail doesn't have -F (which is equivalent to tail -f=name --retry), you could use inotify, and wait for close_write (inotifywait is part of inotify-tools):


while inotifywait -qq -e close_write "$foo" >/dev/null; do
    cat "$foo"
done > log

tail -F should be preferred if available, because there is a race condition when using inotifywait.

  • tail -F is available but I can't get rid of the file truncated messages, even with tail -q -F. Also I think tail -F mangles the output somehow if certain states are written and overwritten very quickly. So I used the solution with inotifywait -qq together with a > /dev/null for the close_write to get rid of the "CLOSE" messages and an extra echo to get values on separate lines. Commented Nov 23, 2013 at 16:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .