December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories

December 2024
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Tomcat log automatic deletion implementation

Tomcat log automatic deletion implementation

ackground

In the production environment, Tomcat generates a lot of logs every day. If you don’t clean up the disk capacity, it will be enough. Manual cleaning is too much trouble. Therefore, write a script to delete the log files 5 days ago (depending on the actual situation).

Writing a script

  1. Write a /usr/local/script/cleanTomcatlog.sh script

!/bin/bash

export WEB_TOMCAT1=/usr/local/tomcat1/logs
export WEB_TOMCAT2=/usr/local/tomcat2/logs
export WEB_TOMCAT3=/usr/local/tomcat3/logs
echo > ${WEB_TOMCAT1}/catalina.out
echo > ${WEB_TOMCAT2}/catalina.out
echo > ${WEB_TOMCAT3}/catalina.out
find ${WEB_TOMCAT1}/* -mtime +5 -type f -exec rm -f {} \;
find ${WEB_TOMCAT2}/* -mtime +5 -type f -exec rm -f {} \;
find ${WEB_TOMCAT3}/* -mtime +5 -type f -exec rm -f {} \;

  1. Set the cleanTomcatlog.sh script to execute
    chmod a+x cleanTomcatlog.sh
  2. Enter the following command
    crontab -e on the console
  3. Press i to edit this text file, enter the following, restart tomcat every day at 4:30 am

Press esc to exit editing, enter wq and enter to save
30 04 * * * /usr/local/script/cleanTomcatlog.sh

Press esc to exit editing, enter wq and enter to save.

The restart timer task

[the root @]

# the crond STOP-Service [the root @] # the crond Start-Service

Name explanation

Explain the crontab and find commands

Crontab
can set the execution schedule of the program through crontab, for example, let the program execute at 8 o’clock every day, or every 10 o’clock on Monday.
crontab -l lists the schedule;
crontab -e to edit schedule;
crontab -d deletion schedule; “the -l” nothing to say, is a view of it; “-e” is the editor,

and vi no difference (in fact, vi is editing a specific file); “-d” basic need, because it put all the user’s schedule are removed, usually do not put a timetable for progressive deleted with “-e” editor; that How to edit it? crontab file format is: MHD md CMD. A 6 field, the last CMD is the program to be executed, such as cleanTomcatlog.sh. M: minute (0-59) H: hour (0-23) D: date (1-31) m: month (1-12) d: one day of the week (0-6, 0 for Sunday) these five fields separated by a space of time which can be a digital value, may be a plurality of numbers separated by commas (or other), if there were not set,

the default is “*.” For example, every day 04 points 30 points execution cleanTomcatlog.sh, is == 30 04 * * * /usr/local/script/cleanTomcatlog.sh==.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>