Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect that no one is writing to a file in Linux?

I am wondering, is there a simple way to tell whether another entity has a certain file open for writing? I don't have time to use iNotify continuously to wait for any current writer to finish writing. I need to do an intermittent check. Thanks.

like image 874
user561440 Avatar asked Oct 13 '22 18:10

user561440


2 Answers

What exactly are you doing where you "don't have time to use iNotify continuously"? First, you should be using the IN_CLOSE_WRITE flag so that iNotify just make one notification when the file gets closed after being written. Using it continuously makes no sense. Second, if your timing is that critical, I'm thinking writing to a file isn't your ideal solution. Do you control the first writer? Do you have to worry about anything else writing to the file after the first writer closes it?

like image 50
Karl Bielefeldt Avatar answered Nov 28 '22 03:11

Karl Bielefeldt


lsof LiSts Open Files. fuser also works similarly (File USER), by telling you which user is using the file.

See: http://www.refining-linux.org/archives/23/16-Introduction-to-lsof-and-fuser/

like image 36
dr jimbob Avatar answered Nov 28 '22 03:11

dr jimbob