Aliyun centos7, tomcat8 configure catalina.out date segmentation and delete it regularly

Foreword: In the environment of tomcat, the log will become huge with the growth of time, sometimes even dozens of G in size, it is very inconvenient to open and view, so here is the log of catalina.out of tomcat to be divided, the cutting tool is used cronolog, and periodically delete some previous logs to ensure memory size.

Environment: centos7 server, tomcat version is above 8 (other versions may be different)

1. Install cronolog (some servers come with this tool)

yum install -y cronolog httpd

2. To configure the cutting file of tomcat, you only need to configure the catalina.sh file in the tomcat directory.

Edit catalina.sh, in the bin directory of tomcat

vim /opt/tomcat2/bin/catalina.sh

Modify three places:

(1). Find

if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/catalina.out
be

change into

if [ -z "$CATALINA_OUT" ] ; then
CATALINA_OUT="$CATALINA_BASE"/logs/%Y-%m-%d.catalina.out
be
It is to split the catalina.out in the logs directory by date

(2). Comment out

#touch "$CATALINA_OUT"

(3). Find

org.apache.catalina.startup.Bootstrap "$@" start \
>> "$CATALINA_OUT" 2>&1 "&"

change into:

org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/sbin/cronolog "$CATALINA_OUT" >> /dev/null&

org.apache.catalina.startup.Bootstrap "$@" start 2>&1 \
| /usr/sbin/cronolog "$CATALINA_OUT" >> /dev/null&

Note : There are two places in the if judgment here, both of which have been changed. Try not to comment the previous one, delete it directly, and replace it with the following code, but you can comment it out and try it out, it is generally not wrong, I am in When testing, it may be that the annotation is not standardized, and the execution is always unsuccessful, and it is deleted later. Then: after wq saves and exits, restart the server to check that the startup is normal. I don’t know why, sometimes it fails to restart after the configuration is completed. However, I still delete the previous code and copy it again. Repeat it a few times and it will be successful. It may be mine. Code is not standardized. Sometimes copying the code in the code box reports an error, you can copy the line of code below.

Screenshot after restart:


The output in the red box indicates that the configuration is successful. Some startup methods are different, and the effect is different. To see if the code is effective, go to see if the log file is divided.

3. Check whether the segmentation is successful

Enter the logs directory of tomcat

cd /opt/tomcat2/logs
ls

Seeing such a file means that the cutting is successful.

4. Periodically delete

Linux comes with a crontab timed task tool, which can be used directly. It is set to execute the deletion task at 00:00 in the morning every day, and delete the log files from a week ago.

 (1). Create a sh script to perform deletion. I put it in the /opt/tomcat/bin directory for easy management

cd /opt/tomcat2/bin
vim auto_del_log.sh

 (2). Copy the following code into it, :wq save and exit

#!/bin/sh
find /opt/tomcat2/logs/ -mtime +7 -name "*.out" -exec rm -rf {} \;
find /opt/tomcat2/logs/ -mtime +7 -name "*.out" -exec rm -rf {} \;

(3). Make timed tasks

crontab -e #Direct this command, add a scheduled task plan

Then add the following code inside:

00 00 * * * /opt/tomcat2/bin/auto_del_log.sh >/dev/null 2>&1

This set up successfully. Go back and execute tomcat2/bin every morning, and the shell script of autoa-del_log.sh in the directory executes the clearing task.

 (4). View timed tasks

crotab -l
Log splitting and periodic deletion is complete!




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325346846&siteId=291194637