(友好提示:本文屬於初級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高手可忽略此文,或者提出改進建議或意見。日誌