linux定时清理日志文件shell脚本

查找并删除七天以前的文件

find /xxx/logs -name '*.log' -and  -mtime +7 -type f |xargs rm

定时清理任务:

23 59 * * * root  find /xxx/logs -name '*.log' -and  -mtime +7 -type f |xargs rm   //(每天23:59分开始执

shell脚本方式:

    vim /xxx/deletelog.sh
    #!/bin/bash          
    workdir=("/export/Domains" "/home/admin")#可填写多个路径
    for wdir in ${workdir[@]}
     do
            echo  filepath is $wdir
            if [ $wdir =  ${workdir[0]} ] ;then
                    fileStr=`find  $wdir/*/logs -type d`
                    echo files is $fileStr
             else
                    fileStr=`find  $wdir -type d`
                    echo filee is $fileStr
            fi
            for dir in $fileStr
            do
                    echo file name is $dir         
                    find $dir -name '*log*' -and -mtime +7  -type f | xargs rm
                     if [ $? -eq 0 ];then
                            echo $date delete $dir success!             
                     else
                            echo $date delete $dir FAILD!            
                     fi
            done
    done

赋给deletelog.sh 文件执行权限

chmod 700    /xxx/logs/deletelog.sh

编辑crontab文件:

 * 9 * * * root /xxx/deletelog.sh >> /export/xxx/deletelog.log  //(带输出日志)

猜你喜欢

转载自blog.csdn.net/weixin_34362991/article/details/87012743