Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quick way to know if a file is open on Linux?

Tags:

python

linux

Is there a quick way (i.e. that minimizes time-to-answer) to find out if a file is open on Linux?

Let's say I have a process that writes a ton a files in a directory and another process which reads those files once they are finished writing, can the latter process know if a file is still being written to by the former process?

A Python based solution would be ideal, if possible.

Note: I understand I could be using a FIFO / Queue based solution but I am looking for something else.

like image 958
jldupont Avatar asked Nov 19 '25 01:11

jldupont


1 Answers

You can of course use INOTIFY feature of Linux, but it is safer to avoid the situation: let the writing process create the files (say data.tmp) which the reading process will definitely ignore. When the writer finishes, it should just rename the file for the reader (into say .dat). The rename operation guarantees that there may be no misunderstandings.

like image 98
fork0 Avatar answered Nov 20 '25 16:11

fork0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!