设置log rotation避免tomcat catalina.out文件增长过大

本文介绍如何通过logrotate工具实现Tomcat日志的自动管理和压缩,包括配置每日轮转、保留文件数量限制及大小控制等关键设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建logrotate配置文件

$ vi /etc/logrotate.d/tomcat

 

添加以下内容:

/opt/tomcat/logs/catalina.out {  
  copytruncate  
  daily  
  rotate 7
  compress  
  missingok  
  dateext
  size 100m  
} 

 

参数说明:

specify the path to the log file. 
copytruncate – creates a copy of the log file and then truncates the original to an empty file so that the service can keep on logging uninterrupted.
daily – rotates the catalina.out daily.
rotate – keeps at most 7 log files. 
compress – create a gzip compressed file of the rotated files.
missingok – suppresses error messages if the file does not exist. 
dateext – add the date to the filename of the archived log file.
size – rotates if the size of catalina.out is bigger than 100m.  

  

在大多数linux系统上都可以找到文件:/etc/cron.daily/logrotate,该文件每天都会被运行。运行时会调用:

/usr/sbin/logrotate /etc/logrotate.conf 

这样就会rotate掉tomcat的日志。
如果有多个tomcat实例,就需要配置多个单独的rotation文件。

 

也可以手动编写定时任务:

2 * * * * /usr/sbin/logrotate /etc/logrotate.d/tomcat

  

也可以顺便将tomcat的临时文件删除掉:

/opt/tomcat/logs/catalina.out {  
  notifempty
  copytruncate
  dateext
  daily
  rotate 10
  compress
  missingok
  postrotate
    /bin/nice /usr/bin/find /opt/tomcat/temp -type f -mtime +10 -exec /bin/rm {} \; > /dev/null
  endscript
}

  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值