Search code examples
pythonloggingviminotify

inotify IN_CLOSE_WRITE fires only once for manually edited file


I'm running python with gevent and gevent_inotifyx to watch a file for any modification using IN_CLOSE_WRITE mask to wait for the file to be closed after write. I get my event fired first time but not after that. I use vim to manually edit the file.

Used a different log file to watch, and that works as expected. This is a python logging file and any time log file is changed with content I get an event fired.

Has anyone come across this situation before? Could this be due to some sync'ing or flushing to disk?


Solution

  • By default, Vim writes the file contents to a temporary file and then moves that over the original, see :help backup. Since this is now a different file (handle), your events stop firing.

    You can make Vim override the original file by setting

    :set backupcopy=yes
    

    If you want to handle this generally, you'd have to monitor the file's directory instead and watch for file moves, too.