linux - 停止 shell 脚本发送重复的电子邮件 - 使用文件标志

标签 linux bash shell

我的要求是,如果我在日志文件中找到字符串,则发送电子邮件;但是,我应该只发送一次。下面贴出我写的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如果您有兴趣。

此外,变量前面至少需要有一个$。通常它们也被 {} 包围。

PS。您可以为帖子使用更好的格式,这样人们就不会给您投反对票。

关于linux - 停止 shell 脚本发送重复的电子邮件 - 使用文件标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42627553/

相关文章:

shell - 在哪里可以了解 "shell:"URI?

linux - 内核、内核线程和用户线程之间的区别

linux - 我正在尝试 grep 文件夹并为结果创建一个变量以供进一步需要

linux - 最大套接字描述符值

json - 如何使用jq选择多个具有重复项目的项目?

java - 如何使用bash或java将三个不同的sqlite3数据库D1、D2和D3附加到另一个数据库D4

shell - 画一棵圣诞树

c - 包含c文件和在C中作为参数给出有什么区别?

linux - 在算术表达式中,为什么递增变量会修改原始变量而其他操作不会?

bash - 后跟条件控制运算符时在子 shell 中忽略 set -e