Skip to content

Instantly share code, notes, and snippets.

@9kopb
Forked from sumonst21/inotify_email_watcher.sh
Created October 8, 2022 15:28
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 9kopb/17ff23670662ac29f49758a6c8423793 to your computer and use it in GitHub Desktop.
Save 9kopb/17ff23670662ac29f49758a6c8423793 to your computer and use it in GitHub Desktop.
#
# Monitor file $1 for changes
# Send an alert emai to $2 if file $1 changes
# usage: inotify_email_watcher.sh /var/log/messages your.name@domain.com
if [ -z "$2" ]; then
echo "Usage: inotify_email_watcher.sh"
exit 1
fi
# if the file exists
if [ -f $1 ] || [ -d $1 ]; then
# Checking if this file or directory is being monitored.
if [ $(ps aux | grep inotifywait | grep -c "$file" ) -gt '0' ]; then
echo "The file or directory is being monitored already: $(ps aux | grep inotifywait | grep "$file" ) ";
exit 1;
fi
echo "Monitoring $1"
inotifywait -m -r -e create,delete,modify,move,attrib $1 | while read FILE
do
echo "A change has been detected in $1"
mail -s "A change has been detected in $1" $2< /dev/null
done
else
echo "File or directory $1 not found."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment