There are many ways to cut Catalian logs, such as shell scripts, cronolog or configuration log4j, and so on. But today I'm going to introduce the use of logrotate to generate logs by date.
If the size of the catalina.out exceeds 2G, Tomcat may crash without error prompting and cannot start. So in order to avoid this problem, we should divide catalina.out every day.
How to cut the catalina.out automatically or when the size reaches 5M per day catalina.out
1. The first method
#!/bin/sh# need to split the log path, the script is generally only modify this path can be used Log_path=/home/webapp/tomcat/logs# get current date D='Date +%y-%m-%d'#获取30天前的日期d90='date-d' -Day ago'+%y-%m-%d'#cd到log_path路径下 and copy the log file to the Cron directory CD ${log_path}&& CP Catalina. outCatalina. out. $d. log# empty the original log file echo> Catalina. out#移除30天前的日志文件rm-RF $log _path/cron/catalina. out. ${d90}.log
- * * * SH script directory * /* *.sh : Wq Save and exit. Crontab timed task configuration complete. You can check whether the setting is successful by crontab-l
The second type:
1. Create a new file
Create a new file in the/etc/logrotate.d/directory, and name it arbitrarily.
1/etc/logrotate.d/tomcat
2. Copy the following statement to the new file above
1/var/log/tomcat/catalina.out { copytruncate daily rotate 7 compress Missingok size 5M }
Specific meanings of each configuration:
- Make sure /var/log/tomcat/catalina.out points to the address of the catalina.out in your server
- Daily -cut daily
- rotate -up to seven files reserved
- Compress -compress the split file
- size -split when Catalina.out is greater than 5M
How it works
- Every night, cron daemon automatically performs tasks under the /etc/cron.daily directory.
- This will trigger the /etc/cron.daily/logrotate file, Logrotate is usually installed with Linux. It will execute this command-"/etc/sbin/logrotate/etc/logrotate.conf" (note space)
- /etc/logrotate.conf contains all the scripts in the /etc/logrotate.d directory
- This will trigger the previous step you wrote the /etc/logrotate.d/tomcate file
Perform logrotate manually
Execute the following statement to run the cron task manually
1/usr/sbin/logrotate/etc/logrotate.conf
More operations for Logrotate
1 Mans logrotate
Log timing cut, and timed deletion (2 methods)