Or is there still a very small window between the command exits and data written to the file?
No, there isn't.
I'd like to read the file right after the command exits, but I do not want to read an empty file.
You can read the final contents of the file right after the command exits, you will never be reading the empty file instead. (In C and C++, use the wait, waitpid, wait3 or wait4 system calls to wait for the program to exit, and only then read the file. If you are using a shell, another programming language or a library (e.g. the C library call system or the Java Process class), it probably uses one of these system calls already.)
As other answers and comments have pointed out, you may end up reading an empty file after the exit of the program if the program has exited without flushing its internal output buffers (e.g. because of _exit, abort or receiving a fatal signal, or because it's a Java program exiting normally). However there is nothing you can do about this at this point: the unflushed data is lost forever, additional waiting won't recover it.
1It probably executes the command right away, but the amount of time it takes to actually open the file, write, and close will depend on the speed and type of your hard drive, any running programs, etc. – freginold – 2018-01-25T12:44:04.707
In terms of the given example, what is 'the process'? Are
echo
and>
not seperate (short lived) processes? And where does the output ofecho
remain before>
is executed? – oɔɯǝɹ – 2018-01-25T17:26:58.373If the user closes the program assume the user doesn't care about the data loss. – Uğur Gümüşhan – 2018-01-25T19:19:30.510
1@oɔɯǝɹ
>
is shell redirection. It is the same as if the program had opened the named file for writing and replaced stdout with it which is exactly what the shell does. – Dan D. – 2018-01-26T00:11:22.4977I think it is the OS responsibility to give you the
file
containingHello
regardless of whether it is flushed or not. – Salman A – 2018-01-26T06:11:23.693@Eric Is your example command part of some shell script and the reading command somewhat later within the same shell script? And what shell do you use? – mvw – 2018-01-27T12:35:19.827
1If the program is running on machine A, and you are reading the file on machine B, with machine A's filesystem mounted over the network, then you may end up reading an empty file, depending on the network filesystem type and the mount settings. So you may want to disable caching for that mount. – pts – 2018-01-27T13:56:25.057
FYI Some programs such as
echo
,cat
anddd
don't use write caching internal to the process, so their output shows up immediately in the output file. – pts – 2018-01-27T13:58:15.580I am a bit scared what this has turned into. :-) – mvw – 2018-01-28T23:20:25.030
I think this question needs some clarification: Is the file stored on a network share (e.g. NFS, Samba) or the local machine? Is the process involved in reading the file sitting on the same computer or a different machine on the network than the one that creates the file? The answers supplied so far assume the same computer, but different machines on a network is a possibility. When networked machines are involved, things quickly get a lot more complicated (e.g. partial files, network issues, caching). – CubicleSoft – 2018-01-29T03:26:20.303