tomcat分割日志并定时清除

配置方法

#安装crontab
yum install cronie -y
#每天凌晨1点1分执行(注意Llinux定时器的时间设置是从分开始,没有秒的)
echo "1 1 * * * /app/tomcat/logs/dellog.sh " >> /var/spool/cron/root

dellog.sh 内容

#!/bin/bash
#定义tomcat日志文件目录 
log_path=/app/tomcat/logs  
#定义当前日期
d=`date +%Y-%m-%d`
#定义15天前的日期
d15=`date -d'15 day ago' +%Y-%m-%d`
#跳转到tomcat日志文件夹,复制catalina.out到catalina.out.${d}.log
cd ${log_path} && cp catalina.out ${log_path}/catalina.out.${d}.log
#清空cat文件alina.out
echo > catalina.out
#删除15天前的catalina.out.${d15}.log
rm -rf ${log_path}/catalina.out.${d15}.log
find ${log_path} -mtime +15 -name "localhost_access_log.*" -exec rm -rf {} \;
find ${log_path} -mtime +15 -name "localhost.*" -exec rm -rf {} \;
find ${log_path} -mtime +15 -name "manager.*" -exec rm -rf {} \;
find ${log_path} -mtime +15 -name "catalina.*" -exec rm -rf {} \;
find ${log_path} -mtime +15 -name "host-manager.*" -exec rm -rf {} \;

参考博客

Linux 定期切割Tomcat catalina.out和清理log日志:https://www.cnblogs.com/wobuchifanqie/p/12790660.html

posted @ 2020-09-27 15:14  鸣昊  阅读(572)  评论(0编辑  收藏  举报