需求:开发要求每5秒在最新的日志里面匹配相应的日志内容,匹配到之后就重启程序。注:程序重启之后会产生新的日志。
#!/bin/bash
logfile=/var/log/bcc_dcoker_err.log
#filename=`ls -t |head -n1|awk '{print $0}'`
sleep=5
function ReDokcer(){
echo "===========================" >> $logfile
date +"%Y-%m-%d %H:%M.%S">> $logfile
if [ -d /home/bcchain/log ];then
cd /home/bcchain/log
grep -rl "DockerLib Run ContainerStart Error" `ls -t |head -n1|awk '{print $0}'`
if [ $? -eq 0 ] ;then
systemctl restart docker && systemctl restart bcchain && systemctl restart tmcore
echo "Restart docker and bcchain and tmcore." >>$logfile
else
echo "Docker is OK." >>$logfile
fi
else
echo "The log directory does not exist." >>$logfile
exit 1
fi
}
count=1
until [ $count -gt 12 ];do
ReDokcer
sleep $sleep
count=$((count + 1))
done
exit 0