Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.