Tomcat 日志切割脚本
编写 log_cutting.sh
脚本
- 本站下载
# wget https://file.cnxiaobai.com/Linux/Shell/log_cutting.sh
- 或手动编辑
# vim log_cutting.sh
# 写入以下内容
#!/bin/bash
DAY=`date +%Y-%m-%d`
YESTERDAY=`date -d last-day +%Y-%m-%d`
# for i in /usr/local/tomcat/目录下的每个Tomcat的名字,例:tomcat-mune 即 for i in mune,配合下面的$i取值
# 多个Tomcat时,则:for i in mune mune2 mune3,即在/usr/local/tomcat/目录下拥有tomcat-mune,tomcat-mune2,tomcat-mune3三个Tomcat服务
# 单个Tomcat时可直接替换为Tomcat所在的绝对路径
for i in mune
do
cat /usr/local/tomcat/tomcat-$i/logs/catalina.out > /usr/local/tomcat/tomcat-$i/logs/catalina.out.$YESTERDAY.back
> /usr/local/tomcat/tomcat-$i/logs/catalina.out
# 删除7天前的日志备份文件
find /usr/local/tomcat/tomcat-$i/logs -name "host-manager.*" -mtime +7 -exec rm -rf {} \;
find /usr/local/tomcat/tomcat-$i/logs -name "localhost.*" -mtime +7 -exec rm -rf {} \;
find /usr/local/tomcat/tomcat-$i/logs -name "manager.*" -mtime +7 -exec rm -rf {} \;
find /usr/local/tomcat/tomcat-$i/logs -name "catalina.*" -mtime +7 -exec rm -rf {} \;
find /usr/local/tomcat/tomcat-$i/logs -name "localhost_access_log.*" -mtime +7 -exec rm -rf {} \;
done
脚本授权
# chmod +x log_cutting.sh
执行脚本
# ./log_cutting.sh
设置定时任务
# crontab -e
# 写入以下内容
# 每天执行一次
0 0 * * * /usr/local/tomcat/log_cutting.sh
评论
0 评论