1

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?

AndrewVT
  • 335
  • 1
  • 8

1 Answers1

1

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I'm accepting your answer, you gave me what I asked for. However, you are quite correct that this is overkill now. Any thoughts on how to make my script a little more intelligent since I don't really need to to fire 20 times in a row if I drop 20 files in a directory? I really just need that edge case of while it is running covered. – AndrewVT Jan 01 '15 at 01:31
  • I can't think of a better way. There doesn't seem to be a way to ask `inotifywait` to tell you about changes that happened since the last time you ran it. Putting a rate-limiter in `sync.sh` seems to be the best solution. – Barmar Jan 01 '15 at 05:37