Skip to content

Instantly share code, notes, and snippets.

@mainakibui
Forked from amorken/inotify-trigger.sh
Created May 25, 2018 07:40
Show Gist options
  • Save mainakibui/5f5c0aebe588285e3be55696f32a418d to your computer and use it in GitHub Desktop.
Save mainakibui/5f5c0aebe588285e3be55696f32a418d to your computer and use it in GitHub Desktop.
Triggering a command when a file tree is changed using inotify
#!/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