我的要求是,如果我在日志文件中找到字符串,则发送电子邮件;但是,我应该只发送一次。下面贴出我写的shell脚本;但是,即使条件不匹配,它也会通过 cron 作业发送重复的电子邮件。
#!/bin/bash
filexists=""
lbdown=""`enter code here`
if [ -f "/var/run/.mailsenttoremedy" ];
then
filexists=true
else
filexists=false
echo filexists is $filexists
fi
if tail -1000 /usr/ibm/tivoli/common/CTGIM/logs/trace.log | grep "Root exception is java.net.NoRouteToHostException: No route to host"
then
echo error found
lbdown=true
echo lbdown status after if in tail is $lbdown
else
lbdown=false
echo lbdown status after else in tail is $lbdown
fi
if filexists=false && lbdown=true
then
{
mailx -S intrelay.sysco.com -r xxx@yyy.com -s "**DEV ALERT**Load Balancer Connection not Available" -v xxx@yyy.com < /dev/null
date > /var/run/.mailsenttoremedy
}
fi
if filexists=true && lbdown=true
then
{
echo MAIL ALREADY SENT
}
fi
if lbdown=false
then
rm -f /var/run/.mailsenttoremedy
fi
echo lbdown is $lbdown and filexists is $filexists
echo 输出是:
filexists is false
Root exception is java.net.NoRouteToHostException: No route to host
error found
lbdown status after if in tail is true
Null message body; hope that's ok
Mail Delivery Status Report will be mailed to <xxx@yyy.com>.
MAIL ALREADY SENT
lbdown is false and filexists is true
最佳答案
您可以尝试对 if
请求进行正常声明...
Bash 格式:
if [ $(tail -1000 /usr/ibm/tivoli/common/CTGIM/logs/trace.log | grep "Root exception is java.net.NoRouteToHostException: No route to host") != "" ];
then
if [ "$filexists" = "false" ] && [ "$lbdown" = "true" ];
then
if [ "$lbdown" = "false" ];
then
通过 if
的测试命令应该被 [
和 ]
包围,至少是具有多个条件的命令。这是guide for if
and some sample codes如果您有兴趣。
此外,变量前面至少需要有一个$
。通常它们也被 {
和 }
包围。
关于linux - 停止 shell 脚本发送重复的电子邮件 - 使用文件标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42627553/