Skip to content

Instantly share code, notes, and snippets.

@caelifer
Last active November 10, 2016 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caelifer/df892f54933e5698269155cd6c19bf7f to your computer and use it in GitHub Desktop.
Save caelifer/df892f54933e5698269155cd6c19bf7f to your computer and use it in GitHub Desktop.
Sample Linux filesystem create event watcher based on inotify(7) framework.
#!/bin/bash
handle() {
file=$1
if [ -f $file ]
then
{
# Send on the background
echo "Started processing '$file'"
let "n = $RANDOM % 9 + 1"
sleep .$n
echo "Done converting '$file' in 0.${n}s"
} &
else
echo "[watch] WARN: '$file' not a file" >&2
fi
}
if [ $# -lt 1 ]
then
printf "\nUsage:\n\t%s <directory>\n\n" $(basename $0) >&2
exit 1
fi
inotifywait -m -q -r $1 -e create --format "%w%f" | while read f
do
handle $f
done
err=$?
if [ $err -ne 0 ]
then
echo "[watch] ERROR: exit status not zero: $err"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment