如果我在日志文件中找到字符串,我的要求是发送电子邮件;但是,我应该只发送一次。我写的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
回声输出是:
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
答案 0 :(得分:0)
您可以尝试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。你可以使用更好的格式来发布帖子,这样人们就不会对你进行投票。
答案 1 :(得分:0)
对于其他人,工作代码是:
#!/bin/bash
filexists=""
lbdown=""
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 "nn"
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
echo removing file
fi
echo lbdown is $lbdown and filexists is $filexists