监控服务日志的shell脚本

发布时间:2019-09-04编辑:脚本学堂
要求写个脚本,如果超过2分钟没有产生新log则发送邮件给admin@localhost.localdomain...

假设/var/log/a.log是某程序的log文件
内容形如:2012-07-27 18:12:17,788 INFO [com.kmsocial.search.websearch.WeiboPool:229] - Weibo pool running!
要求写个脚本,如果超过2分钟没有产生新log则发送邮件给admin@localhost.localdomain。完整代码如下:
 

复制代码 代码如下:

#!/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
!
}

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<&-