Created
April 12, 2018 07:28
-
-
Save msh01/d7a1551bc1e8912a831a5ec4e61d0740 to your computer and use it in GitHub Desktop.
自动删除tomcat下超过7天的日志,以及tomcat目录下的log4j日志
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 /software/common-shell/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=/software/tomcat | |
# the keep days of log-files.[config value range: 2 -- 365] 配置日志保留天数 | |
KEEP_LOGFILE_DAYS=7 | |
API_PROJECT="gh-iu-rest-api" | |
C_PROJECT="gh-iu-c" | |
B_PROJECT="gh-iu-b" | |
Y_PROJECT="gh-iu-y" | |
# 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} | |
} | |
# | |
removeExpiredLog4jFiles() { | |
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") | |
# gh-iu-rest-api.log.2017-07-20 | |
# LOG_FILE_NAME=${log_file_prefix_name}${CUR_DATE}${log_file_ext_name} | |
LOG_FILE_NAME=${log_file_prefix_name}${log_file_ext_name}.${CUR_DATE} | |
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=$? | |
removeExpiredLogFiles "${log_dir}" "localhost_access_log." ".txt" | |
f=$? | |
if [ ${a} -eq 1 -a ${a} -eq ${b} -a ${a} -eq ${c} -a ${a} -eq ${d} -a ${a} -eq ${e} -a ${a} -eq ${f} ]; 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 循环“查找匹配到 tomcat- 字样的目录和文件” | |
for t in `find $SEARCH_DIR -name 'tomcat-*'` | |
do | |
# 判断是否为目录 | |
if [ -d "${t}/logs" ]; then | |
writelog " ${t}/logs/" ; | |
removeExpiredLogFilesForTomcat "${t}/logs" ; | |
# 平滑清空庞大的catalina.out文件 | |
echo "" > "${t}/logs/catalina.out" ; | |
# 删除当前tomcat下的项目log4j日志 | |
removeExpiredLog4jFiles "${t}/logs/${API_PROJECT}" "${API_PROJECT}" ".log" | |
removeExpiredLog4jFiles "${t}/logs/${C_PROJECT}" "${C_PROJECT}" ".log" | |
removeExpiredLog4jFiles "${t}/logs/${B_PROJECT}" "${B_PROJECT}" ".log" | |
removeExpiredLog4jFiles "${t}/logs/${Y_PROJECT}" "${Y_PROJECT}" ".log" | |
fi | |
done | |
writelog "#-------------------------------------------------END" | |
writelog "" | |
unset -v SEARCH_DIR KEEP_LOGFILE_DAYS EXECUTE_LOG_FILE | |
unset -f writelog removeExpiredLogFiles removeExpiredLogFilesForTomcat |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment