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, 增加行:
30 3 * * *
/home/scripts/logmonitor.sh > /home/scripts/lcron.log 2>&1
每天3:30執行
該指令碼