2017-03-06 1 views
-2

내 요구 사항은 로그 파일에서 문자열을 찾으면 전자 메일을 보내는 것입니다. 그러나 나는 그것을 한 번만 보내야한다. 필자가 작성한 쉘 스크립트는 아래에 붙여 넣습니다. 그러나 조건이 일치하지 않아도 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 [email protected] -s "**DEV ALERT**Load Balancer Connection not Available" -v [email protected] < /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 <[email protected]>. 
MAIL ALREADY SENT 
lbdown is false and filexists is true 

답변

0

당신은 if 요청에 대한 정상 선언을 시도 할 수 ...

배쉬 형식 :

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입니다.

또한 변수는 최소한 앞에 $이 필요합니다. 일반적으로 그들은 {}으로 둘러싸여 있습니다.

추신. 게시물에 대해 더 나은 형식을 사용하여 사람들이 당신을 다운 다운시키지 않도록 할 수 있습니다.

+0

내가 "당신"의와 모든 "U"의 교체의 자유를했다. –

+1

노력에 감사 드리며 나는 더 나은 약속을 얻습니다! – suleiman

+0

나는 그것을 마침내 만들었다, 고마워. 만약 내가 잘못 선언했다면 쉘은 문법 오류를 던지지 않고 모든 것을 내부로 전달할 수 있다고 생각합니다. – Bazooka

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 [email protected] -s "**DEV ALERT**Load Balancer Connection not Available" -v [email protected] < /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 
관련 문제