自动日志清理脚本程序
#!/bin/sh
########################################################## #created by ydds # #date 2011-4-25 # #function log clear default save 7 days logs # #when hard disk space achieve 80%, automatic save 3 days # #when hard disk space achieve 90%,save only LOG today # #when only today log hard disk space achieve >95% # # clear today log # ########################################################## set -xoldboyWarnValue=80
oldboyCriticalValue=90 oldboyCC=95 nohupFlag=0# $1 must be directory
if [ $# -ne 2 ]||[ -z $1 ]||[ ! -d $1 ];then echo "Uage:logClear /home/oldboy/apachelogs /dev/xvda1" exit 1 fi#clear log path
logPath=$1devstr=$(df|awk '/\//{print $1}'|sed -n '/\//p')
echo "${devstr}"|grep -q "$2" [ $? -ne 0 ]&&exit 1# hard disk name
oldboyName=$2scriptLog=/server/script/clearLog.log
#clear nohuplog
clearNohupLog() { if [ ${nohupFlag} -ne 1 ];then # check nulup.log >1G clear fileNohupLog="${logPath}/../nohup.log" NohupSize=$(du ${fileNohupLog}|awk '{print $1/1024/1024}'|awk -F "." '{print $1}') echo "check nohup.log size: $(du -h ${fileNohupLog}|awk '{print $1}')">>${scriptLog} if [ -f ${fileNohupLog} ];then if [ ${NohupSize} -ge 1 ]||[ $1 -eq 1 ];then echo "">${fileNohupLog} if [ $? -eq 0 ];then echo "clear nohup.log ok!">>${scriptLog} fi fi fi fi }#hard disk used
oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}')echo "[$(date +%Y-%m-%d\ %H:%M:%S)]check hard disk used start....">>${scriptLog}
echo "check current hard disk space used ${oldboyUsed}%">>${scriptLog}#clear nohup.log >warning
if [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then echo "hard disk used ${oldboyUsed}% > ${oldboyWarnValue}%,start clear nohup.log..">>${scriptLog} clearNohupLog 1 nohupFlag=1 fiif [ ${nohupFlag} -eq 1 ];then
#hard disk used echo "after deal nohup.log ,check hard disk again...">>${scriptLog} oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}') fi#save 7,3 days logs
clearnDayLogs() { # cd tmp directory cd /tmpif [ $1 -ne 1 ];then
if [ -d ${logPath} ];then cd ${logPath} find . -type f -mtime +$1 |xargs rm -f if [ $? -eq 0 ];then echo " clear ${logPath} logs $1 ago days logs successfully!">>${scriptLog} else echo " clear ${logPath} logs $1 ago days logs fail!">>${scriptLog} fi fi else #clear nohup.log clearNohupLog 1clearFileNameArr=($(ls ${logPath}|awk -F "." 'NF==3{print $0}'))
# clear file name is not blank if [ -n "${clearFileNameArr}" ];then echo "|">>${scriptLog} for((i=0;i<${#clearFileNameArr[@]};i++)) do if [ ${i} -eq 0 ];then echo "clear file------------------start--------------------------->">>${scriptLog} fi # cd this directory cd ${logPath} if [ -f "${logPath}/${clearFileNameArr[$i]}" ];then find . -type f -name ${clearFileNameArr[$i]} |xargs rm -f fiif [ $? -eq 0 ];then
echo "${clearFileNameArr[$i]}">>${scriptLog} fi done echo "clear file------------------------- end -------------------->">>${scriptLog} echo "|">>${scriptLog} else echo "☆☆☆☆☆☆ Not files 1 day ago ☆☆☆☆☆☆">>${scriptLog} fi afterClearoldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}') echo "clear ${logPath} logs ${1} ago days logs successfully!">>${scriptLog} fi }
如果你把握不好该脚本带来的误删除的风险,请慎用。
#when today log save ,check hard disk used >80%,so clear today logs clearTodayLog() { oldboyUsedTmp=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}') echo "clear today log start ...">>${scriptLog} #todayFileArr=($(ls ${logPath})) todayFileArr=($(find ${logPath} -type f|awk -F "/" '{print $NF}' )) echo "start save current logs tail 300 ..">>${scriptLog} echo "|">>${scriptLog} echo "dealing currnet logs name*********start*************>>>>">>${scriptLog} for((i=0;i<${#todayFileArr[@]};i++)) do cd ${logPath} #get tail 300 --> tmp file tail -300 ${todayFileArr[$i]}>/tmp/tmpydds #check file exist again save tail 300 --- this file
if [ -f "${logPath}/${todayFileArr[$i]}" ];then
find . -type f -name ${todayFileArr[$i]} -exec cat /tmp/tmpydds >${logPath}/${todayFileArr[$i]} \; if [ $? -eq 0 ];then echo " ${todayFileArr[$i]}----- ok!">>${scriptLog} else echo " ${todayFileArr[$i]}----- fail!">>${scriptLog} fi fi done echo "dealing currnet logs name********* end *************>>>>">>${scriptLog} echo "|">>${scriptLog} if [ $? -eq 0 ];then echo "clear today log successfully!">>${scriptLog} else echo "clear today log fail!">>${scriptLog} fi } wflag=0 cflag=0 flag=0 count=0 #clear nohup.log #clearNohupLog 0while true
do oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}') #not first and hard disk < warning value break [ ${count} -ne 0 ]&&[ ${oldboyUsed} -lt ${oldboyWarnValue} ]&&breakcount=1
################next if [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then case ${cflag} in 1) clearTodayLog cflag=2 ;; 2) echo "baojing---------->">>${scriptLog} break ;; esaccase ${wflag} in
1) clearnDayLogs 1 wflag=2 ;; 2) clearTodayLog wflag=3 ;; 3) echo "baojing---------->">>${scriptLog} break ;; esac fi######################first
if [ ${wflag} -eq 0 ]&&[ ${cflag} -eq 0 ];then#when >95% clear today
if [ ${oldboyUsed} -ge ${oldboyCC} ];then#clear 1 ago log
clearnDayLogs 1#clear today log
clearTodayLog break#when hard disk is used 90% is Critical today log only save
elif [ ${oldboyUsed} -ge ${oldboyCriticalValue} ];then echo "${oldboyName} current used ${oldboyUsed}%>= ${oldboyCriticalValue}% is Critical!">>${scriptLog} cflag=1 clearnDayLogs 1#when hard disk is used ${oldboyWarnValue}% is Warning automatic save 3 days
elif [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then echo "${oldboyName} current used ${oldboyUsed}%>= ${oldboyWarnValue}% is Warnning!">>${scriptLog} wflag=1 clearnDayLogs 3#when hard disk is used lt 80% the logs oldboy is normal.
else echo "${oldboyName} current used ${oldboyUsed}% is Normal!">>${scriptLog} clearnDayLogs 7 break fi fiflag=1
done#when clear all log oldboy used
oldboyUsed=$(/bin/df ${oldboyName}|tail -1|awk '{print $5}'|awk -F "%" '{print $1}') #after deal log check disk usedif [ ${oldboyUsed} -ge ${oldboyCC} ];then
echo "oldboy used ${oldboyUsed}% is Critical Critical!">>${scriptLog}elif [ ${oldboyUsed} -ge ${oldboyCriticalValue} ];then
echo "oldboy used ${oldboyUsed}% is Critical!">>${scriptLog}elif [ ${oldboyUsed} -ge ${oldboyWarnValue} ];then
echo "oldboy used ${oldboyUsed}% is Warning!">>${scriptLog}else
echo "oldboy used ${oldboyUsed}% is Normal!" fiecho "check hard disk used end!">>${scriptLog}
本文出自 “” 博客,请务必保留此出处