Shell 脚本自动检测系统日志错误并发送邮件通知
#!/bin/bash
'定义变量' LOG_FILE='/var/log/syslog' ERROR_COUNT=0
'检查日志文件中是否有错误信息' if grep -q 'error' $LOG_FILE; then ERROR_COUNT=$(grep -c 'error' $LOG_FILE) fi
'发送邮件通知管理员' if [ $ERROR_COUNT -gt 0 ]; then SUBJECT='[Error Alert] There are $ERROR_COUNT errors in syslog' BODY='Please check the syslog for more details' echo $BODY | mail -s $SUBJECT admin@example.com fi
'记录检测时间和错误数量' echo "$(date) - Found $ERROR_COUNT errors in syslog" >> /var/log/error_check.log
'结束脚本' exit 0
原文地址: https://www.cveoy.top/t/topic/mVLg 著作权归作者所有。请勿转载和采集!