Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use Shell script to realize timing cutting of tomcat Log

Shulou Source: shulou.com Published: 2022-06-01 08:44:45 09月22日 Update

This article shows you how to use Shell script to achieve tomcat log regular cutting, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.

The significance of log slicing:

In a production environment, when we use tomcat services, if there are too many users, there is no log cutting, there will be a very large log, a day's log can have several G size. It's very troublesome when we need to check the log records. Therefore, log cutting is very necessary!

1. Write shell scripts

Vim fengelog.sh

# script statement #! / bin/bash. / etc/profile. ~ / .bash_profile# definition log file directory applog_path=/usr/local/apache-tomcat-app/logssapplog_path=/usr/local/apache-tomcat-sapp/logs# definition date format d = `date +% Y-%m-%d-%H:% M`d15 = `date-dong15 day ago' +% Ymuri% d` # 15 days date # cutting log cd ${applog_path} & & cp catalina.out $log # log Back up echo > catalina.out # original log clear rm-rf $applog_path/$ {d15} * .log # keep 15-day log cd ${sapplog_path} & & cp catalina.out $sapplog_path/$d.log # log backup echo > catalina.out # original log clear rm-rf $sapplog_path/$ {d15} * .log # keep 15-day log 2. Execute shell script

Chmod + x fengelog.sh

Sh fengelog.sh

3. Add a scheduled task

Crontab-e

0 * / 3 * sh / root/fengelog.sh # set to cut the log every 3 hours

Note:

1. The beginning of the script. / etc/profile,. ~ / .bash_profile is to enable the scheduled task to successfully execute the added environment variables, if not added, the scheduled task will not be executed!

two。 The log retention time and the number of tomcat logs cut at a time, and how long the log is cut, can be customized.

3. It is recommended that after the script is written, run it yourself first to make sure there is no problem, and join the scheduled task!

The above content is how to use Shell script to achieve tomcat log regular cutting, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Logs scripts tasks content backups skills date time environment knowledge large concise necessary successful concise variables size hours that is suggestions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Microsoft Linux vpn Shulou Technology