the file's content is shown as empty as long as the file is opened for writing by Bash.
That's not exactly what's happening. What's happening is that the task buffers its output — it is accumulated in memory for a while, then written to the file in chunks. The in-memory buffer has a fixed size of a few kilobytes. This is done for performance, as each file write has a relatively high overhead. So you won't see your program's output at the beginning, but you will see it come piece by piece. This has nothing to do with bash.
If you want to see the output immediately, you can start your program with unbuffer
(from the expect
software package). You trade performance for immediacy.
A second issue is that cat
will show the output that has already been emitted, and exit as soon as it reaches the end of the file. If you want to keep reading output when it's appended, use tail
's follow mode.
nohup unbuffer task >out.txt &
tail -n +1 -f out.txt
Press Ctrl+C to exit tail
. Instead of tail
, you can use less
and its follow mode (press F
; press Ctrl+C to stop reading).
ext4
with default options it should be updated after every 4096 bytes written. – Paweł Rumian Jul 16 '14 at 14:46