Search code examples
linuxbashinotifywait

inotifywait misses events while script is running


I am running an inotify wait script that triggers a bash script to call a function to synchronize my database of files whenever files are modified, created, or deleted.

#!/bin/sh

while inotifywait -r -e modify -e create -e delete /var/my/path/Documents; do
  cd /var/scripts/
  ./sync.sh
done

This actually works quite well except that during the 10 seconds it takes my sync script to run the watch doesn't pickup any additional changes. There are instances where the sync has already looked at a directory and an additional change occurs that isn't detected by inotifywait because it hasn't re-established watches.

Is there any way for the inofitywait to trigger the script and still maintain the watch?


Solution

  • Use the -m option so that it runs continuously, instead of exiting after each event.

    inotifywait -q -m -r -e modify -e create -e delete /var/my/path/Documents | \
        while read event; do
            cd /var/scripts
            ./sync.sh
        done
    

    This would actually have the opposite problem: if multiple changes occur while the sync script is running, it will run it again that many times. You might want to put something in the sync.sh script that prevents it from running again if it has run too recently.