AIX 定时清除日志的SHELL

1、AIX 机器上定期清除日志文件,以释放日志文件所占用的大量磁盘空间。

shell似乎没有处理日期计算的函数,在JAVA中处理日期很方便,而SHELL处理日期太费劲,等于自己要写一个原始的日期加减函数。这里考虑特定的应用,在已有网友代码的基础上进行了一下改进,可以计算从当前日期起N天前的日期。KSH实现。

AIX 5.3 执行通过

#!/bin/ksh
LOG_PATH=/home/utan/logs
LOG_DEL_LOGFILE=./logdel.log
KEEP_DAYS=9

# Set the current month day and year.
month=`date +%m`
day=`date +%d`
year=`date +%Y`

#get the yesterday
getLastDay()
{
# Set the current month day and year.
# month=`date +%m`
# day=`date +%d`
# year=`date +%Y`
year=$1
month=$2
day=$3

# Add 0 to month. This is a
# trick to make month an unpadded integer.
month=`expr $month + 0`

# Subtract one from the current day.
day=`expr $day - 1`

# If the day is 0 then determine the last
# day of the previous month.
if [ $day -eq 0 ]; then

# Find the preivous month.
month=`expr $month - 1`

# If the month is 0 then it is Dec 31 of
# the previous year.
if [ $month -eq 0 ]; then
month=12
day=31
year=`expr $year - 1`

# If the month is not zero we need to find
# the last day of the month.
else
case $month in
1|3|5|7|8|10|12) day=31;;
4|6|9|11) day=30;;
2)
if [ `expr $year % 4` -eq 0 ]; then
if [ `expr $year % 400` -eq 0 ]; then
day=29
fi
else
day=28
fi
;;
esac
fi
fi

case $day
in 1|2|3|4|5|6|7|8|9) day='0'$day
esac
case $month
in 1|2|3|4|5|6|7|8|9) month='0'$month
esac
# echo $year$month$day
}

# cacl the date dividing value
caclDividingValue()
{
echo keeydays:$KEEP_DAYS
i=1
while [ $i -le $1 ]
do
#echo $i
getLastDay $year $month $day
i=$(($i+1))
done
}

#delete the old logs before the keep days
caclDividingValue $KEEP_DAYS
sdate="$year$month$day"
echo The log files will be deleted before $sdate.
for file in `ls $LOG_PATH`
do
# echo here$file
if [ "$file" -lt "$sdate" ]; then
echo $LOG_PATH/$file
snow=`date`
echo [$snow] delete $LOG_PATH/$file >> $LOG_DEL_LOGFILE
rm -fr $LOG_PATH/$file
fi
done

2、加上定时处理功能,输入crontab -e, 增加行:

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->30 3 * * * /home/scripts/logmonitor.sh > /home/scripts/lcron.log 2>&1

每天3:30执行 该脚本

你可能感兴趣的:(xml,脚本,AIX)