一個shell監控服務日志的腳本
假設/var/log/a.log是某程序的log文件
他的內容是:
2012-07-27 18:12:17,788 INFO [com.kmsocial.search.websearch.WeiboPool:229] - Weibo pool running!
要求寫個腳本,如果超過2分鐘沒有產生新log則發送郵件給
[email protected]
#!/bin/bash
LOGFILE="/var/log/a.log"
STARTNUM=`wc -l $LOGFILE`
mail_to_root(){
mail -s "weibo pool has some problem" root<<!
there has no logs in two minutes!
!
}
exec 6<"$LOGFILE"
while :
do
NEWNUM=`wc -l $LOGFILE`
if [ "$STARTNUM"_ = "$NEWNUM"_ ];then
mail_to_root
else
STARTNUM=$NEWNUM
fi
while read -u 6 new_log
do
if echo "$new_log" | grep -q "Weibo pool running";then
while read -u 6 tmp
do
:
done
break
else
mail_to_root
fi
done
sleep 120
done
exec 6<&-