Skip to content

Instantly share code, notes, and snippets.

@raoulduke
Last active August 19, 2021 17:32
Show Gist options
  • Save raoulduke/d2e00bb0723b986cddd04c27d525542f to your computer and use it in GitHub Desktop.
Save raoulduke/d2e00bb0723b986cddd04c27d525542f to your computer and use it in GitHub Desktop.
Log monitor with email
#!/bin/bash
# Monitor file for changes and send email notification
# Usage: ./logmond.sh /path/to/file recipient@domain.com
usage() {
echo "Usage: ./logmond.sh /path/to/file recipient@domain.com"
}
if [ "$#" -ne 2 ]; then
usage
exit 1
fi
echo "Monitoring file ($1) for changes, email will be sent to ($2)"
cur_line_count="$(wc -l < $1)"
while true
do
new_line_count="$(wc -l < $1)"
if [ "$cur_line_count" != "$new_line_count" ]
then
lines_changed=$((new_line_count - cur_line_count))
changes="$(tail -n $lines_changed $1)"
echo "$changes" | s-nail -s "logmond Alert for $1" $2
fi
cur_line_count="$new_line_count"
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment