使用Linux中的shell脚本监视日志并发送警报邮件

时间:2016-10-11 07:13:36

标签: linux shell email

我创建了.sh文件来grep日志中的特定单词,并将邮件发送到特定的emailID(Set Cronjob每1小时运行一次这个脚本)。它按预期工作,但问题是如果没有错误,我也会收到空邮件。

如果我们发现错误并且DBError文件包含内容,则需要获取Mail。请帮助我解决这个问题。

    #!/bin/bash

if [ ! -e DBErrors ] ; then
      grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > DBErrors
      mail -s "ALERT: sqlException" Jayaram.Ponnusamy@gmail.com < DBErrors
else
  comm -23 <(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log) DBErrors | mail -s "ALERT: sqlException" Jayaram.Ponnusamy@gmail.com
  grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > DBErrors
fi

由于 贾亚拉姆

1 个答案:

答案 0 :(得分:0)

你能尝试一下吗?

#!/bin/bash
dbErrors="/tmp/DBErrors"
if [ ! -e "$dbErrors" ]; then
        grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > $dbErrors
        mailbody=$(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log); [[ -n "$mailbody" ]] && mail -s "ALERT: sqlException" Jayaram.Ponnusamy@gmail.com
else 
        mailbody=$(comm -23 <(grep "sqlException" /opt/apps/cms/logs/cms-runtime.log| sort -n) <(sort -n "$dbErrors")); [[ -n "$mailbody" ]] && mail -s "ALERT: sqlException" Jayaram.Ponnusamy@gmail.com
        grep "sqlException" /opt/apps/cms/logs/cms-runtime.log > $dbErrors
fi