linux下tomcat日志文件增长导致磁盘空间已满

2021年4月7日 5点热度 0条评论 来源: Bin Xiao

linux下tomcat日志文件增长导致磁盘空间已满

因无规划,将tomcat直接放在/目录下的/usr/local/tomcat,日志文件也存在默认的路径,当日志增长,可用空间为20KB

检查发现catalina.out日志也没有做log rotation

解决方法有两个。

一、更改tomcat下logs的路径(因/home目录有134G,已用1%)

Tomcat安装目录下Logs目录下默认会产生4种日志文件:catalina.*.log,host-manager.*.log,localhost.*.log,manager.*.log。系统出现异常的时候,有时候log文件会暴涨。或开发不注意将log rotation

找到Tomcat安装目录下conf目录中的logging.properties文件,修改

1catalina.org.apache.juli.FileHandler.level = FINE

1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

1catalina.org.apache.juli.FileHandler.prefix = catalina.

 

2localhost.org.apache.juli.FileHandler.level = FINE

2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

2localhost.org.apache.juli.FileHandler.prefix = localhost.

 

3manager.org.apache.juli.FileHandler.level = FINE

3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

3manager.org.apache.juli.FileHandler.prefix = manager.

 

4host-manager.org.apache.juli.FileHandler.level = FINE

4host-manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs

4host-manager.org.apache.juli.FileHandler.prefix = host-manager.

 

将${catalina.base}/logs修改成指定目录:/home/tomcat/logs  

 

二、对日志进行截断(不管放到哪个路径下,建议都对日志进行截断,也即logrotate)

创建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文件。

 

手动编写定时任务:

crontab -e

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

}

 

    原文作者:Bin Xiao
    原文地址: https://blog.csdn.net/xiaohuibin0541/article/details/84889476
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。