Created
June 28, 2012 10:42
-
-
Save amorken/3010625 to your computer and use it in GitHub Desktop.
Triggering a command when a file tree is changed using inotify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Watch a directory and run a script every time inotify reports that something | |
# changed, followed by 10 seconds of no changes. Handy for responding to incoming | |
# rsync jobs or the like. | |
# | |
# Requires that inotify-tools is installed. | |
WATCHDIR=${1:./} | |
TRIGGERCOMMAND=${2:-echo fired} | |
# Wait for something to happen... | |
while inotifywait -qq -r -e modify,close_write,create,delete,move,delete_self $WATCHDIR ; do | |
# something happened, waiting for nothing to happen for 10 seconds | |
while inotifywait -t 10 -qq -r -e modify,close_write,create,delete,move,delete_self $WATCHDIR ; do | |
# more stuff happened, hold off a while longer | |
sleep 1 | |
done | |
WAITSTATUS=$? | |
#echo waitstatus: $WAITSTATUS | |
if [[ "$WAITSTATUS" == "1" ]] ; then | |
echo inotifywait returned an error. Aborting. | |
exit 1 | |
fi | |
$TRIGGERCOMMAND | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment