写一个shell脚本,每隔30秒查看一第二天记文件/data/logs/error.log,看有没有新的日记,若是有,须要发邮件告警。python
判断时间点,若是是 0 点 0 分需考虑特殊状况,记录上一第二天记的行数,对比本第二天记行数和上一第二天记行数;shell
假设:1) 该日记0点0分会清空 2) 邮件脚本路径/usr/local/sbin/mail.pybash
思路:用 while 循环, sleep 30,spa
[root@localhost_002 shell]# cat 12.sh #!/bin/bash while : do t=`date +%H%M` if [ $t == "00" ] #判断是不是00: then rm -fr /tmp/log_line.txt sleep 60 fi if [ ! -f /tmp/log_line.txt ] then touch /tmp/log_line.txt wc -l /data/log/error.log|awk '{print $1}' >> /tmp/log_line.txt fi last_n=`tail -1 /tmp/log_line.txt` #新增日记的行; n=`wc -l /data/logs/error.log|awk '{print $1}'` #总日记的行; echo $n >> /tmp/log_line.txt if [ $n -gt $loat_n ] then python /usr/local/sbin/mail.py yuanhh@163.com "错误日记增长" “tail /data/log/errlor.log” fi sleep 30 done
第二个方法:本身整理:code
#!/bin/bash mail=yuanhh@163.com logfile=/data/logs/error.log t1=`date +%H` t1=`date +%M` n=`cat $logfile |wc -l` while : do n1=`cat $logfile |wc -l` if [ -z $n ] || [ -z $n1 ] then echo "The value none" exit fi if [ $n1 -gt $n ] then /usr/local/sbin/mail.py $mail "error.log " "errorlog 增长" fi if [ $t1 == "00" -a $t2 == "00" ] then echo $n > /tmp/1.log echo $n1 >> /tmp/1.log fi sleep 30 done