再贴一段shell,以供参考
1 #! /bin/bash 2 DEPLOY_PATH=$(cd "$(dirname "$0")"; pwd) 3 INI_FILE=$DEPLOY_PATH'/cnedmp_etl.ini' 4 . $INI_FILE 5 BASE_PATH=$(cd "$(dirname "$DEPLOY_PATH")"; pwd) 6 MAIN_PATH=$(cd "$(dirname "$BASE_PATH")"; pwd) 7 CURRRENT_DATE=`date -u +\%Y\%m\%d` 8 CURRENT_TIME=`date -u +"%Y%m%d%T"` 9 LOG_PATH=$BASE_PATH'/log/' 10 RECORD_FILE=$LOG_PATH'emailrecord.rcd' 11 12 #echo $value | sed 's/[0-9]\{1,\}/30/g' 13 #sed -i 's/[0-9]\{1,\}/30/g' test.txt 14 #sed -i 's/^Load_Check.20170803.log.*[0-9]\{1,\}/Load_check.20170803.log 178/g' emailrecord.rcd 15 16 cd $LOG_PATH 17 test -e $RECORD_FILE || touch $RECORD_FILE 18 19 loglist=`ls $LOG_PATH | grep "${CURRRENT_DATE}.log"` 20 #echo $loglist 21 22 23 #Use to store email cotent 24 email_message="" 25 for logname in $loglist 26 do 27 # echo $logname 28 prc_exist=`cat $RECORD_FILE | grep "$logname"` 29 test -z "${prc_exist}" 30 if [ $? -eq 0 ]; then 31 prc=0 32 echo "$logname `cat $logname | wc -l`" >> $RECORD_FILE 33 else 34 prc=`echo $prc_exist | awk -F ' ' '{print $NF}'` 35 nrc=`cat $logname | wc -l` 36 sed -i "s/^$logname.*[0-9]\{1,\}$/$logname $nrc/g" $RECORD_FILE 37 fi 38 39 tmp_message="" 40 warn_message=`cat $logname | tail -n +$[ $prc + 1 ] | grep "WARN"` 41 test -z "${warn_message}" 42 if [ $? -ne 0 ] 43 then 44 tmp_message="${tmp_message}WARN Message:\\n${warn_message}\\n" 45 fi 46 fatal_message=`cat $logname | tail -n +$[ $prc + 1 ] | grep "FATAL"` 47 test -z "${fatal_message}" 48 if [ $? -ne 0 ] 49 then 50 tmp_message="${tmp_message}FATAL Message:\\n${fatal_message}\\n" 51 fi 52 test -z "${tmp_message}" 53 if [ $? -ne 0 ] 54 then 55 email_message="${email_message}\\n${logname}:\\n${tmp_message}\\n" 56 fi 57 done 58 59 test -z "${email_message}" 60 if [ $? -ne 0 ] 61 then 62 63 #/usr/sbin/sendmail "CN_EDMp_Dev@exchange.sc.com" <<EOF 64 #subject:<Failed job/file etc> 65 #from:EDMpCN.ETL 66 #"Below is WARN and FATAL: \\n"$email_message 67 #EOF 68 69 email_log="/home/infra1/jason/workspace/Logs/emaillog/""sendmail.$CURRENT_TIME.log" 70 echo -e "Below is WARN and FATAL:\\n${email_message}" > $email_log 71 72 fi