tomcat日志切割脚本

tomcat日志每俩小时切割的脚本如下(这是用定时任务来完成的,此方法无需重启tomcat):

[root@localhost crontab]# cat tomcat_log.sh 
#!/bin/bash
#date:2021-12-27

time=$(date +%H) 
end_time=`expr $time - 2`
BF_TIME=$(date +%Y%m%d)_$end_time:00-$time:00
clean_date=$(date +%Y%m%d)_`expr $time - 4`:00-`expr $time - 2`:00
echo $BF_TIME
cp /u01/ofm/apache-tomcat-8.5.23/logs/catalina.out /u01/ofm/apache-tomcat-8.5.23/logs/catalina.$BF_TIME.out
echo " " > /u01/ofm/apache-tomcat-8.5.23/logs/catalina.out

#清除4小时之前的日志
/bin/rm -rf  /u01/ofm/apache-tomcat-8.5.23/logs/catalina.$clean_date.out

# 设置定时任务,每两小时执行一次

crontab -e
0 */2 * * * sh /root/tom_log.sh

 # 最后的效果

catalina.20211227_12:00-14:00.out

 

posted @ 2021-12-27 15:32  梦徒  阅读(192)  评论(0编辑  收藏  举报