Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use Shell script to realize tomcat Log cutting

Shulou Source: shulou.com Published: 2022-06-03 04:44:33 10月03日 Update

How to use Shell script to achieve tomcat log cutting? I believe that most people have not yet learned this skill, in order to let you learn, to give you a summary of the following content, do not say much, let's move on.

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:

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!

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!

After reading this article, have you learned how to use Shell scripts to cut tomcat logs? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel. Thank you for reading.

Tags: Logs scripts tasks content backups skills date time more environment large necessary success variables size most hours suggestions beginning meaning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info vpn Xiaomi Huawei