简体   繁体   English

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

[英]Monitor log and send Alert mail using shell script in Linux

I created .sh file to grep specific words in log and will send a mail to particular emailID (Set Cronjob to run this script every 1hour). 我创建了.sh文件来对日志中的特定单词进行grep,然后将邮件发送到特定的emailID(将Cronjob设置为每1小时运行一次此脚本)。 Its working as expected, But the Problem is if there is no error also I am getting Empty Mail. 它的工作正常,但是问题是如果没有错误,我也会收到空邮件。

Needs to get Mail, if we caught error and DBError file has content. 如果我们发现错误并且DBError文件包含内容,则需要获取Mail。 Kindly please help me on this to resolve this issue. 请对此提供帮助,以解决此问题。

    #!/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

Thanks Jayaram 谢谢贾亚拉姆

Could you try this; 你可以尝试一下吗?

#!/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 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 linux shell 脚本中发送邮件,使用邮件,还有 awk? - How can I send a mail in linux shell script, using mail, but also awk? 寻找 shell 脚本来监视远程 java 进程并在进程被杀死时发送电子邮件警报 - looking for shell script to monitor the remote java process and send an email alert when process got killed 用于每天监控文件夹的Shell脚本,如果未在该文件夹上生成新文件,则邮件警报将发送 - Shell script to monitor a folder daily basis, if new file is NOT GENERATED on that folder, Mail alerts will send 从 linux shell 脚本发送邮件 - Sending a mail from a linux shell script 在Linux中,如何使用ssh和Expect在远程服务器执行期间监视shell脚本的进度 - In Linux , how to monitor the progress of shell script during execution in remote server using ssh and expect shell脚本来监视日志中的“单词”,然后终止并重新启动进程 - shell script to monitor a “word” in log, then kill and restart process 远程发送shell脚本到linux机器 - Send shell script to linux machine remotely Bash脚本监视日志,匹配关键字然后发送命令,恢复监视 - Bash script to monitor log, match keyword then send commands, resume monitoring Linux:试图监控不同的 linux 文件系统大小并向 2 个或更多人发送邮件 - Linux: Trying to monitor different linux filesystem sizes and send mail to 2 or more people 需要使用其他Linux服务器的“ sendmail”发送邮件(bash或php脚本) - Need to send a mail (bash or php script) using “sendmail” of a different linux server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM