Skip to content

Instantly share code, notes, and snippets.

@wnasich
Last active August 29, 2015 14:19
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 wnasich/ab20b63755a0fe3190b0 to your computer and use it in GitHub Desktop.
Save wnasich/ab20b63755a0fe3190b0 to your computer and use it in GitHub Desktop.
Report changes in log file by email
#!/bin/bash
# Adapted from http://stackoverflow.com/a/4657776/641892
file=$1
subject=$2
size=0
offset=0
recipients="foo@bar"
if [[ $file =~ / ]]; then
echo "$0 does not accept path components in the file name" 2>&1
exit 1
fi
if [[ -e .offset.$file ]]; then
offset=$(<".offset.$file")
fi
if [[ -e $file ]]; then
size=$(stat -c "%s" "$file") # this assumes GNU stat, possibly present as gstat. CHECK!
# (gstat can also be Ganglias Status tool - careful).
fi
if (( $size < $offset )); then # file might have been reduced in size
echo "reset offset to zero" 2>&1
offset=0
fi
echo $size > ".offset.$file"
if [[ -e $file && $size -gt $offset ]]; then
tail -c +$(($offset+1)) "$file" | head -c $(($size - $offset)) | mail -s "$subject ($file)" $recipients
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment