Timeline for What Linux shell command can I use to display content of a file in which another process is writing?
Current License: CC BY-SA 4.0
Post Revisions
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| 4 hours ago | history | edited | Amazon Dies In Darkness | CC BY-SA 4.0 |
edited body
|
| 20 hours ago | comment | added | CSM |
tail -f (follow) will display new lines when the OS sees them; as others have said this is subject to buffering
|
|
| 21 hours ago | history | became hot network question | |||
| yesterday | comment | added | Kamil Maciorowski |
The answer with flush() is good, this is the right thing to do. Nevertheless that answer does not answer any of your explicit questions. The answer to the title is "cat or tail you used; the fact you did not see the expected lines means they were not (yet) in the file". After learning about buffering and how to improve your program, do you still want to see lines you were expecting to see without stopping the process? If not then the question shall be rebuild to "why don't I see lines I expect and what to do to be able to?". Also see: XY problem.
|
|
| yesterday | comment | added | grawity | @LPChip: That is true for "DOS share modes" which still exist in Windows (where a program needs to specifically opt in to sharing the file for reads, or reads/writes, when it is opening the file) but no such thing exists on Linux. | |
| yesterday | answer | added | spikey_richie | timeline score: 12 | |
| yesterday | comment | added | Kamil Maciorowski | @LPChip AFAIK the mandatory lock feature in Linux was unreliable, optional since Linux 4.5 (2016) and no longer supported in 5.15 (2021) and above. But even when it kinda worked, a program that wanted to lock a file had to request a lock explicitly, I think. And then another program that wanted to read a locked (fragment of a) file would either block or get an error. If my hypothesis about the write buffer is right then your advice (closing the file) will probably work; still closing and reopening the file again and again does not seem to be a good practice, the OP may just need to flush. | |
| yesterday | comment | added | LPChip | As far as I know, when a program is holding a write-lock on a file, you can't read from it. So what you would want to do is change your program so everytime it needs to write data, it opens the file for writing, append to the file, and close the filehandle. This should allow you to cat or tail the file and get the most recent data out of it. | |
| yesterday | history | asked | schlebe | CC BY-SA 4.0 |