自动清除过时的Tomcat日志的shell脚本

(友好提示:本文属于初级shell编程,高手可忽略此文)shell

    在产品环境运行过程当中,天天会切割产生按天计的日志文件,这些日志文件通常在过必定期限之后就没什么用处了,所以须要按期删除掉这些过时的日志文件。本文以CentOS下的Tomcat为应用环境基础,经过shell脚本的方式定时自动删除过时的Tomcat日志文件,以减小磁盘空间的占用、减轻管理员的平常工做。apache

   1、shell脚本代码:编程

#!/bin/bash
#
# filename: clearExpiredTomcatLogs.sh
#
# FUNCTION: clear the expired tomcat log files
#
# -----------------增长 crontab 定时任务
# 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 + :" and input "wq", press "Enter" key to exit the crontab
# -----------------

# the base directory for search the existed apache tomcat. 配置包含tomcat目录的路径,该目录或其子孙目录下存在Tomcat目录
SEARCH_DIR=/products/dds/

# the keep days of log-files.[config value range: 2 -- 365] 配置日志保留天数
KEEP_LOGFILE_DAYS=31

# execute log for this shell 配置本脚本的执行日志文件
EXECUTE_LOG_FILE=${SEARCH_DIR}clear-expired-tomcat-logs.log

##
# write execute log 写日志信息到本脚本的执行日志文件中
writelog() {
    if [ ! -f "${EXECUTE_LOG_FILE}" ]; then
        touch ${EXECUTE_LOG_FILE}
    fi
    echo "$1">>${EXECUTE_LOG_FILE}
}

##
# remove expired log files 移除过时的日志文件(此方法为被调用方法);可根据实际须要 在删除前 增长日志备份功能
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 移除过时的tomcat的日志文件(此方法为被调用方法);若有其余日志文件可增长删除条目
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"
    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 directories:"

##
# find the apache-tomcat and remove the expired log files 循环“查找匹配到 apache-tomcat 字样的目录和文件”
for t in `find $SEARCH_DIR -name '*apache-tomcat-*'`
do
    # 判断是否为目录
    if [ -d "${t}/logs" ]; then
        writelog "    ${t}/logs/"
        removeExpiredLogFilesForTomcat "${t}/logs"
    fi
done

writelog "#-------------------------------------------------END"
writelog ""

unset -v SEARCH_DIR KEEP_LOGFILE_DAYS EXECUTE_LOG_FILE
unset -f writelog removeExpiredLogFiles removeExpiredLogFilesForTomcat

   2、shell脚本的执行tomcat

   可按照脚本内容中“增长 crontab 定时任务”的描述增长为系统的定时任务;也可直接在命令行窗口中直接运行,如: sh clearExpiredTomcatLogs.sh  或者 在授予可执行权限 (chmod +x clearExpiredTomcatLogs.sh) 后执行 ./clearExpiredTomcatLogs.shbash

   3、特别说明this

   shell脚本中应尽可能保持没有中文字符,以免产生莫名问题。以上shell脚本中的中文仅为了解释相关内容,实际使用的脚本中是没有相关内容的。命令行


以上shell脚本应该是比较简单的,shell高手可忽略此文,或者提出改进建议或意见。日志

相关文章
相关标签/搜索