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.

Sometimes, tailing an output log which is constantly being updated doesn't give the whole lines. Why is that?

grep pattern input_file > output.log &
tail output.log

Why doesn't it print the last line in full?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

Reading and writing a file is not line-atomic, and tail -f is not line-buffered. So, when tail -f reads the file while it was still being written, it may reach the end of the file (and therefore stop printing) before the process writing to the file writes the end of the line. When that happens, it prints an incomplete line because that's all it sees.

share|improve this answer
    
If you any source links, can you please post those links? I would like to do further reading on that. Thanks for reply. –  user14039 Mar 14 '12 at 18:16
    
@tech_learner Look into race conditions. The article is more focused on single variables, but the same principles apply to whole files (even more so, perhaps). –  Kevin Mar 14 '12 at 18:23

Your Answer

 
discard

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