Skip to content

Instantly share code, notes, and snippets.

@aguilard07
Created November 20, 2021 13:32
Show Gist options
  • Save aguilard07/12e84e16566622807e6d79879548992c to your computer and use it in GitHub Desktop.
Save aguilard07/12e84e16566622807e6d79879548992c 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
@fhdalikhan
Copy link

fhdalikhan commented Dec 30, 2021

hello thanks @aguilard07 for this, but how do I stop this once I run it? as when I do
ps aux | grep inotifywait
I see that the process id keeps changing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment