Shell script cuts Tomcat’s log file

Time:2021-5-4

Since the file cutting of logback and log4j has not been successful in debugging, we can write a script with the shell to cut the log file under Tomcat (if you have successfully used the file cutting in logback or log4j, you can leave the usage, thank you first)

1: Cut the crap and paste the script directly:

#!/bin/sh
log_dir=/var/log/tomcat
monitor_ File = $1 ᦇ absolute path of catalina.out file in Tomcat directory
file_size=`du $monitor_file | awk '{print $1}'`
if [ $file_size -ge 102400 ]
then
if [ ! -d $log_dir ]
then
MKDIR / var / log / Tomcat # create and save the cutting file directory. This path can be modified by yourself and saved to the directory you want
fi
CP $1 / var / log / Tomcat / log - ` date +% Y -% m -% d -% H% m% s'. Log save log file
Echo ` date +% Y -% m -% d -% H:: "file cutting" > > / var / log / Tomcat / split. Log ා record the cutting log
Echo "> $1? Clear the log / catalina.out file of Tomcat
fi

Note: this script is very simple. Linux uses a timer to scan the size of the log file every minute. When the file is larger than 100m, it first copies the file to the specified directory, and then clears the original Tomcat log file. Here is to use crontab to use the timer. As for the syntax of this command, I won’t say much here. If you are interested, you can Google by yourself. There is still a small disadvantage here. Because you scan log files at a fixed time every minute, the final cut file size will still be between 100 and 102m.

Then use the timer to execute the script regularly

2: Edit timer code with crontab – e

*/1 * * * * sh /lee_ js/monitor_ Script.sh/opt/apache-tomcat-7.0.67/logs/catalina.out # the first path is the path of the script, and the second parameter is the absolute path of the catalina.out file under Tomcat

Then save it

So far, the log cutting is done

Note: if there is no crontab command, you can use Yum to install:


yum install vixie-cron
yum install crontabs

The above is the log file of Tomcat cut by shell script introduced by Xiaobian. I hope it can help you. If you have any questions, please leave me a message and Xiaobian will reply you in time. Thank you very much for your support to developer!

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]