Shell scripts that automatically purge expired tomcat logs

Source: Internet
Author: User

(Friendly hint: This article belongs to the elementary shell programming, the Master can ignore this article)

During the operation of the product environment, daily log files are cut to produce the day-to-date logs, which are generally not useful after a certain period of time, and therefore need to be periodically removed from these outdated log files. Based on the application environment of Tomcat under CentOS, this paper automatically deletes outdated tomcat log files by shell script, so as to reduce the usage of disk space and reduce the daily work of administrators.

One, Shell script code:

#!/bin/bash## filename: clearexpiredtomcatlogs.sh## function: clear the expired  tomcat log files## -----------------added  crontab  timed tasks # add sys  schedule:#     crontab -e# press  "I"  enter the modify  mode, and add schedule item in new-line:# 05 00 * *  * /bin/bash /products/dds/clearExpiredTomcatLogs.sh# press  "ESC"  key to  exit Modify mode, then press  "SHIFT&NBSP;+&NBSP;:"  and input  "Wq ", press " Enter " key to exit the crontab# -----------------# the  base directory for search the existed apache tomcat.  Configure the path that contains the Tomcat directory, or the Tomcat directory exists under its descendant directory search_dir=/products/dds/# the keep days of  Log-files. [Config value range: 2 -- 365]  Configuration log retention days keep_logfile_days=31# execute log for this  shell  Configuring the execution log file for this script Execute_log_file=${search_dir}clear-expired-tomcat-logs.log### write execute  log  Write log information into the execution log file of this script writelog ()  {    if [ ! -f  "${ Execute_log_file} " ]; then        touch ${execute_log_ file}    fi    echo  "$" >>${EXECUTE_LOG_FILE}}###  remove expired log files  Remove the Expired log file (this method is called method); can be used according to actual needs   before delete   Add log backup function Removeexpiredlogfiles ()  {    log_dir=$1    log_file_ prefix_name=$2    log_file_ext_name=$3    removed_file=1     LOG_FILE=    LOG_FILE_NAME=    CUR_DATE=     for ((i=${keep_logfile_days};i<=365;i++));do        cur_date=$ (date + "%y-%m-%d"  --date= "-$i  day")         log_file_name=${log_file_prefix_name}${cur_date}${log_file_ext _name}        log_file= "${log_dir}/${log_file_name}"          if [ -f  "${log_file}"  ]; then             writelog  "         ${log_file_name} "            rm -f  ${LOG_FILE}            REMOVED_FILE=0         fi    done    if [  ${REMOVED_FILE} -eq 0 ]; then         writelog  ""     fi    unset -v log_file_prefix_name log_file_ext_name     unset -v log_file log_file_name cur_date    return ${ Removed_file}}### remove the tomcat ' s log files  Remove the log file of the expired Tomcat (this method is called method) ; If you have additional log files, you can add delete entries Removeexpiredlogfilesfortomcat ()  {    log_dir=$1     # remove log-files that which is out of the keep days.     removeExpiredLogFiles  "${log_dir}"   "Catalina."   ". Log"     a=$?    removeExpiredLogFiles  "${log_dir}"   " Catalina. "   ". Out"     b=$?    removeExpiredLogFiles  "${log_dir}"   " Host-manager. "   ". Log"     c=$?    removeExpiredLogFiles  "${log_dir}"   " Manager. "   ". Log" &NBSP;&Nbsp;  d=$?    removeexpiredlogfiles  "${log_dir}"   "localhost."   ". Log"     e=$?    if [ ${a} -eq 1 -a  ${a} -eq ${b} -a ${a} -eq ${c} -a ${a} -eq ${d} - a ${a} -eq ${e} ]; then        writelog  "        # no expired log file"          writelog  ""     fi    unset -v  log_dir}writelog  "#-------------------------------------------------START" writelog  "' Date  + "%y-%m-%d %a %h:%m:%s" ' "writelog " keep days for tomcat log  files:  $KEEP _logfile_days "writelog " remove the expired tomcat log files  in the following d irectories: "### find the apache-tomcat and remove the expired log  files  Loop "Find directories and files matching to  apache-tomcat " for t in  ' find  $SEARCH _dir  -name  ' *apache-tomcat-* ' do    #  to determine if it is a directory     if [  -d  "${t}/logs"  ]; then        writelog  "     ${t}/logs/"         removeexpiredlogfilesfortomcat  "${t}/logs"     fidonewritelog  "#----------------- --------------------------------END "writelog " "unset -v search_dir keep_logfile_days  Execute_log_fileunset -f writelog removeexpiredlogfiles removeexpiredlogfilesfortomcat

Ii. Execution of shell scripts

You can add the timer task to the system according to the description of "Add Crontab timed Task" in the script content, or you can run directly in the command line window, such as: SH clearexpiredtomcatlogs.sh or Grant executable permission (chmod +x clearexpiredtomcatlogs.sh) after execution./clearexpiredtomcatlogs.sh

III. Special Instructions

The shell script should try to keep no Chinese characters in order to avoid creating inexplicable problems. In the shell script above only to explain the relevant content, the actual script used is not relevant content.


The shell script above should be relatively simple, the shell master can ignore this article, or make suggestions for improvement or comments.

Shell scripts that automatically purge expired tomcat logs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.