Last active
November 10, 2016 21:39
-
-
Save caelifer/df892f54933e5698269155cd6c19bf7f to your computer and use it in GitHub Desktop.
Sample Linux filesystem create event watcher based on inotify(7) framework.
This file contains 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 | |
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