日志文件清理代码

Posted 诸子流

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了日志文件清理代码相关的知识,希望对你有一定的参考价值。

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

#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

 

以上是关于日志文件清理代码的主要内容,如果未能解决你的问题,请参考以下文章

我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情

VC++获取文件的修改时间,定期清理若干天之前的日志文件(附源码)

请问如何清理mysql-bin.00001这些日志文件

argparse 代码片段只打印部分日志

Linux定时清理磁盘日志文件——Shell脚本

python常用代码片段总结