Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • 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. Commented yesterday
  • 2
    @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. Commented yesterday
  • 1
    @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. Commented yesterday
  • 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. Commented yesterday
  • 4
    tail -f (follow) will display new lines when the OS sees them; as others have said this is subject to buffering Commented 20 hours ago