Will incron’s IN_CREATE event give me a blank file

inotify

I want to monitor a directory for new files, which will be created by another process. I plan to use incrod to do this.

I am not interested in reacting to changes in files, as the files should never be updated. Therefore the IN_CREATE event seems the sensible option to listen for. However, I am unsure, if I listen for that event, could I end up with an empty file (before the other process has written any information to it)? Should I use the IN_WRITE_CLOSE event instead?

Best Answer

You can observe which events are passed to your directory with inotifywait to check how does it behave when your process is running:

$ inotifywait -m . 
Setting up watches.
Watches established.

For example, after running touch file in same directory:

$ inotifywait -m .
Setting up watches.
Watches established.
./ CREATE file
./ OPEN file
./ ATTRIB file
./ CLOSE_WRITE,CLOSE file

Since there is delay between creating file and closing it, you'll have empty file after CREATE event, as you suspected. Observe events coming after running:

from time import sleep

with open("somefile", 'w') as somefile:
    sleep(15)
    somefile.write("...")

Therefore using CLOSE_WRITE event sounds reasonable.

Related Question