linux 日志定时清理脚本

来源:互联网 发布:python urllib2 post 编辑:程序博客网 时间:2024/06/05 09:57

工作中经常会碰到服务日志占满服务器磁盘,如果不去清理,服务就可能无法正常工作。适当提高日志的打印级别,如info级别提高到warn可以临时缓解下,但也不长久之际,

还得写个脚本定时清理下。

#!/bin/bash#clearLog.shnginxDir=/usr/local/nginx/logs/*devInfo=($(df -l | awk '{print $1}'))     #日志所处的磁盘perInfo=($(df -l | awk '{print int($5)}')) #磁盘使用率for i in `seq 0 ${#perInfo[@]}`;do    if [[ ${devInfo[i]} = '/dev/xvda1' ]] && [[ ${perInfo[i]} -ge 80 ]];   then           for file in $nginxDir;     do         exist=`echo $file | awk '{if(match($0,/\.log/)) print "yes"}'`;         if [[ -f $file ]] && [[ ${exist} = yes ]];         then           echo '' > $file;           echo $(date) $file "clear log ok!" >> /var/log/clear.log ;         fi;     done   fi;    done

确定日志所处磁盘的使用情况,一般使用率达到80%就可以进行清理了。

接着起个定时任务:

crontab -e

输入:

0/30 * * * *  /xxx/clearLog.sh

每隔30分钟检测一次磁盘使用情况



0 0
原创粉丝点击