定时清理过期日志脚本

定时清理指定日期以前的日志文件,防止磁盘满

这里是如果/home下磁盘使用超过85%,则清除15天以前的,如果仍超过85%,则清除14天前的,直到低于85%

#!/bin/bash

clean()
{
  local interval=$1
        find /home/admin/apache/logs \( \( -name "*access_log" -or -name "*error_log" \) -and -ctime +$interval \) -print -type f -exec rm -rf {} \;
}

getsize()
{
        local size=`df -h | grep "/home" | awk '{print $5}' | tr -d '%'`
        return $size
}

getsize
declare -i use=$?
declare -i tag=15

echo "**cleanlogs start@`date +%D` ..."

while [ $use -ge 85 -a $tag -ge 0 ]
do
        echo "**usage: $use%, cleanup: $tag days"
        clean $tag
        getsize
        use=$?
        tag=`expr $tag - 1`
done

getsize
use=$?

if [ $use -ge 85 ]
then
        echo "cleanlogs failed, usage: $use%" 1>&2
else
        echo "**cleanlogs done, usage: $use%."
fi

猜你喜欢

转载自hantao.iteye.com/blog/832382