Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
These questions though not exact duplicates are pretty close and definitely related: unix.stackexchange.com/questions/10454/… & unix.stackexchange.com/questions/83443/… –  slm Nov 23 '13 at 18:55

1 Answer 1

up vote 4 down vote accepted

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

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

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):

file=foo

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.

share|improve this answer
    
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. –  Marcus Junius Brutus Nov 23 '13 at 16:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.