从0开始用shell写一个tomcat日志清理脚本

一、目的

tomcat日志随着时间的流逝会越来越大,虽然我们可以使用cronolog对tomcat输出的日志根据日期进行切割,但是日子一长,进到logs/文件夹下都是密密麻麻的日志,不好查看也浪费了大量的空间,故本文的目的是编写一个脚本,能根据文件名中的日期自动清理过期的日志。(该脚本只使用使用yyyy-MM-dd命名的日志)

二、技术储备

至少过了《Linux Shell脚本攻略(第2版)》这本书,拥有一定的开发知识

三、需求设计

该清理的脚本的逻辑是遍历日志文件夹中的日志,获取文件名中的时间,并判断是否过期,过期便rm,并记录到日志。脚本只是简单的提供“查找-判断-清除-记录”的功能,定时执行脚本交给crontab进行控制,综合以上设计思想,便能在每天执行日志检查清理工作。

四、详细设计与实现

脚本主要由两个模块构成

1.日志记录模块
主要由一个方法构成,其主要逻辑包括创建该清理脚本的日志文件,并能把上一级传递过来的参数按时间输入到日志文件中,要求日志文件能根据日期进行切分。

1.1 脚本

logsPath=/root/clearserver/logs #定义该清理脚本的日志文件夹地址

#定义日志记录函数
function log(){
        #根据日期构建日志路径名
        logPath=$logsPath/clear.`date +%Y-%m-%d`.log
        #判断日志文件夹是否存在,不存在便创建
        if [ ! -d $logsPath  ]; then
                mkdir $logsPath
        fi
        #判断日志文件 是否存在,不存在便创建
        if [ ! -d $logPath ];then
                touch $logPath
        fi
        #构建一个时间格式,并简单的把传递过来的参数 append到日志中
        echo [`date +%Y-%m-%d\ %H:%M:%S`] $1 >> $logPath
}

2.清理模块
清理模块主要的逻辑是
遍历需要清理的日志文件夹里面的日志文件->截取日志文件名里面的日期->判断是否过期->清理,日志记录

2.1 脚本

#设定一个过期时间,以秒为单位
let expire=60*60*24*30

#配置需要清理的日志文件夹路径
files_path=/root/clearserver/test

#读取所有文件并存入变量
files=`ls $files_path`

#使用上述函数进行日志记录
log "start to check"

#累加器,记录删除的日志文件数
count=0

#开始遍历
for file in $files;
do
         fileName=${file##*/} #获取文件名,可去掉,这是优化前的代码

         #截取文件名中的时间
         date=`echo $file|egrep -o '[0-9]*-[0-9]*-[0-9]*'`

         #转换为纪元时
         time=`date +%s -d $date`

         #当前纪元时
         now=`date +%s`

         #时间差
         differ=$[ now - time ]

         if [ $differ -gt $expire ];
         then
                #如果过期,进入文件夹,删除文件
                cd $files_path 
                rm -rf $file
                #日志记录
                if [ $? -eq 0  ];
                then
                        log "clear file : $fileName success ";#do some logic
                        let count++
                else
                        log "clear file : $fileName fail"
                fi
        fi
done
#日志记录
log "end and  cleared $count record"

3.完整脚本与测试

clear.sh

#!/bin/bash
logsPath=/root/clearserver/logs

function log(){
        logPath=$logsPath/clear.`date +%Y-%m-%d`.log
        if [ ! -d $logsPath  ]; then
                mkdir -p $logsPath
        fi
        if [ ! -d $logPath ];then
                touch $logPath
        fi
        echo [`date +%Y-%m-%d\ %H:%M:%S`] $1 >> $logPath
}

#init expire time
let expire=60*60*24*30

#init the logs path
files_path=/root/clearserver/test

#find all logs
files=`ls $files_path`
log "start to check"
count=0
for file in $files;
do
         fileName=${file##*/}
         date=`echo $file|egrep -o '[0-9]*-[0-9]*-[0-9]*'`
         #change to time
         time=`date +%s -d $date`
         #now time
         now=`date +%s`
         differ=$[ now - time ]
         if [ $differ -gt $expire ];
         then
                cd $files_path
                rm -rf $file
                if [ $? -eq 0  ];
                then
                        log "clear file : $fileName success ";#do some logic
                        let count++
                else
                        log "clear file : $fileName fail"
                fi
        fi
done
log "end and  cleared $count record"

3.1 测试准备
在/root文件夹下创建clearserver文件夹,vim 写入脚本 clear.sh
在/clearserver文件夹下创建test文件夹用于模拟tomcat的日志文件夹,进入到test文件夹,使用touch创建几个测试日志文件
准备好了之后你会看到如下图:
这里写图片描述

今日是11月29号,执行清理脚本之后将会清理11月之前的日志

bash clear.sh

这里写图片描述

现在看看生成的清理日志内容
这里写图片描述

现在已经能成功对文件进行清理,只需要把该脚本配置到crontab在每天凌晨2点执行便可每天检测并清理没用日志啦

猜你喜欢

转载自blog.csdn.net/hayre/article/details/78665705