Skip to content

Instantly share code, notes, and snippets.

@Chion82
Created January 9, 2016 20:34
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 Chion82/4dd175eb49d1f8fb3f3d to your computer and use it in GitHub Desktop.
Save Chion82/4dd175eb49d1f8fb3f3d to your computer and use it in GitHub Desktop.
#!/bin/bash
LOG_FILE=/var/log/xiaoyu/error.log
ERROR_KEYWORD="Error"
EMAIL="sdspeedonion@gmail.com"
SUBJECT="Error Report from loveoncampus.cn"
PREFIX="<h2>Error detected on server loveoncampus!</h2><br/>Error log:<br/><div style='background-color:#f1f1f1;padding:10px;'>"
FOLLOW="</div>"
LAST_LOG_TIME=$(stat -c %Y $LOG_FILE)
while true; do
NEW_LOG_TIME=$(stat -c %Y $LOG_FILE)
if [[ $NEW_LOG_TIME != $LAST_LOG_TIME ]] ; then
LOG_TAIL=$(tail $LOG_FILE -n 100)
echo "Error detected:"
echo $LOG_TAIL
echo "Sending notification mail."
echo ${PREFIX}${LOG_TAIL}${FOLLOW} | mailx -s "$(echo -e "${SUBJECT}\nContent-Type: text/html")" ${EMAIL}
fi
LAST_LOG_TIME=$NEW_LOG_TIME
sleep 2
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment