2016年11月17日 | Leave a comment 问题: 发现网站出错了 解决: 需要安装 mail & sendmail/postfix ,可以使用 echo “ttt” | mail -s “mail test” ***@sss.com 测试下邮件发送是否正常 定时执行 TrackNginx500.sh 即可 TrackNginx500.sh 如下: #!/bin/bash #扫描nginx accesslog 如果发现500 则发邮件給fanghua.tang@yek.me 会出现重复发送 errlog=`tail -n 2000 /var/log/nginx/access.log | grep '" [5][0-9][0-9] ' |grep -v ' 499 ' |tail -n 100 ` echo $errlog errcount=`echo $errlog |wc -w` echo "errcount: $errcount" if [ $errcount -gt 0 ] then mv /var/log/nginx/lastemailerrlog /var/log/nginx/lastemailerrlog.2 echo "$errlog" >/var/log/nginx/lastemailerrlog diff /var/log/nginx/lastemailerrlog /var/log/nginx/lastemailerrlog.2 diffret=`echo ${PIPESTATUS[0]}` if [ '0' = $diffret ] then echo '重复跳过发送错误'; else echo '有错误发邮件' echo "$errlog \n"|mail -r 'fanghua.tang@yek.me' -s '服务器nginx 出现500' fanghua.tang@yek.me fi else echo '正常' fi 12345678910111213141516171819202122 #!/bin/bash#扫描nginx accesslog 如果发现500 则发邮件給fanghua.tang@yek.me 会出现重复发送errlog=`tail -n 2000 /var/log/nginx/access.log | grep '" [5][0-9][0-9] ' |grep -v ' 499 ' |tail -n 100 `echo $errlogerrcount=`echo $errlog |wc -w`echo "errcount: $errcount"if [ $errcount -gt 0 ]then mv /var/log/nginx/lastemailerrlog /var/log/nginx/lastemailerrlog.2 echo "$errlog" >/var/log/nginx/lastemailerrlog diff /var/log/nginx/lastemailerrlog /var/log/nginx/lastemailerrlog.2 diffret=`echo ${PIPESTATUS[0]}` if [ '0' = $diffret ] then echo '重复跳过发送错误'; else echo '有错误发邮件' echo "$errlog \n"|mail -r 'fanghua.tang@yek.me' -s '服务器nginx 出现500' fanghua.tang@yek.me fielse echo '正常'fi