方法1:
#!/bin/sh
system=$(date +%s)
echo 系统时间:$system
service=$(stat -c %Y /home/catalina.log)
echo 服务日志时间:$service
dif=$(($system-$service))
echo 时间差异:$dif
if [ $dif-gt 60 ]
then
echo 服务异常,重启服务!
echo ps -ef|grep PRONAME|grep -v 'grep'|awk '{print$2}'
else
echo 时间差异在1分钟区间内,Normal service!
fi
方法2:
#!/bin/sh
curTime=`date +"%Y-%m-%d %H:%M:%S"`
logTime=`tail -1 /home/catalina.log| awk -F' ' '{ print $1 " " substr($2,0,8)}'`
echo -e "Log time: \t$logTime\nCurrent time: \t$curTime"
logSec=`date -d "$logTime" +%s`
curSec=`date +%s`
gapSec=`expr "$curSec" - "$logSec"`
echo 时间差异:$gapSec
if [ $gapSec -gt 60 ]
then
echo 服务异常,重启服务!
echo ps -ef|grep PRONAME|grep -v 'grep'|awk '{print$2}'
else
echo 时间差异在1分钟区间内,Normal service!
fi