首页 > 代码库 > 日志文件清理代码

日志文件清理代码

对于产生大量日志的服务器,要对日志进行控制,如果任由日志膨胀可能会写满磁盘造成服务器宕机。

#code by ls in 20170427
date_now=`date +"%Y-%m-%d"`
date_yes=date -d "1 day ago" +"%Y-%m-%d"
#the drectory of this script,need to change for yourself
clean_log_path="/iom_jk/IOM_INF/tomcat_6.0.44_1/clean_log/"
if ! [ -e ${clean_log_path} ]
then
    mkdir -p $clean_log_path
fi
clean_log_file="${clean_log_path}clean_log.log" #the logfile of this script,need to change for yourself
delete_log_prefix="log.log."            #the prefix of delete file,need to change for yourself
zero_log_postfix=".log"                        #the postfix of zero file,neet to change for yourself
#those drectory that need to clean,neet to change for yourself
array_path[0]="/iom_jk/IOM_INF/tomcat_6.0.44_1/webapps/IomInterface/WEB-INF/sh/logs"
array_path[1]="/iom_jk/IOM_INF/tomcat_6.0.44_1/bin/logs"
array_path[2]="/iom_jk/app/interface/IomInterface/WEB-INF/sh/logs"

delete_log_file()            #the function to delete yesterday log file,do not need to change in general
{
    if [ -e ${delete_log_prefix}${date_yes} ]
    then
        rm -f ${delete_log_prefix}${date_yes} && echo "${data_now} : ${delete_log_prefix}${date_yes} have been delete" >> ${clean_log_file}
    fi
}

zero_log_file()       #the function to zero log file,do not need to change in general
{
    for file_name in `ls *${zero_log_postfix}`
    do
        echo "" > ${file_name} && echo "${data_now} : ${file_name} have been clean" >> ${clean_log_file}
    done
}

for path_log in $array_path[*]
do
    cd $path_log
    echo "$date_now : enter ${path_log}" >> ${clean_log_file}
    delete_log_file                            #the function to delete yesterday log file,need to change for yourself
    zero_log_file                                #the function to zero log file,need to change for yourself
    echo "" >> ${clean_log_file}
done

 

日志文件清理代码