定期清理系统日志脚本
程序分为两部分:
/housekeep/scripts/housekeep_ux.cfg (将需要清理的日志路径和相关配置写入此)
/housekeep/scripts/housekeep_ux.sh (具体的清理执行脚本,设置完成后将housekeep_ux.sh加入cron)
要提前创建归档文件夹/housekeep/logs/archive;也可在housekeep_ux.sh中更改具体路径:ARCHIVE_DIR=/housekeep/logs/archive
/housekeep/scripts/housekeep_ux.cfg
#Log directory|Log file name|Log retention period|Log zip period|Archived /housekeep/logs/archive|*|7|0|noarchive /housekeep/logs|*.log|30|7|noarchive /var/log|mail*|30|1|archive /var/log|scpm|30|1|archive /var/log|warn|30|1|archive /var/log|evms-engine*|30|7|noarchive /var/log|messages|30|1|archive /var/log|*snmpd.log|30|7|archive /var/log|authlog|30|7|archive /var/log|wtmp|30|1|archive /var/log|ntp|30|7|archive /var/log|localmessages|7|1|archive /tmp|*|30|7|noarchive /var/log/sa|sa.*|60|30|noarchive /var/log/sa|sar.*|60|30|noarchive /housekeep/logs|housekeep_ux*.log|30|7|noarchive
/housekeep/scripts/housekeep_ux.sh
#!/bin/ksh # ------------------------------------------------------------------------------ # Script : housekeep_ux.sh # Purpose : This script housekeep all TS supported logs # Prerequisite : /housekeep/logs/archive /housekeep/scripts/housekeep_ux.cfg # ------------------------------------------------------------------------------ CNT=0 LOG="/housekeep/logs/housekeep_ux.`date +%m%d%y%H%M%S`.log" { PATH=/sbin:/usr/sbin:/usr/bin:/bin export PATH # --------- Housekeep logs ------------------------- # --------- Use config file (housekeep_ux.cfg) ---------------- CONFIG=/housekeep/scripts/housekeep_ux.cfg ARCHIVE_DIR=/housekeep/logs/archive GZLIST=/tmp/gzlist.lst RMLIST=/tmp/rmlist.lst # Add a counter to file for achive to avoid overwrite if files with same name in different directories # ------------------------------- Start of program ------------------------------- for i in `cat $CONFIG|grep -v "#"` do dir=`echo $i |cut -d'|' -f1` name=`echo $i |cut -d'|' -f2` deltime=`echo $i |cut -d'|' -f3` ziptime=`echo $i |cut -d'|' -f4` archive=`echo $i |cut -d'|' -f5` DIR="[EMPTY]" if [ $archive = 'archive' ]; then if [ -d $ARCHIVE_DIR ]; then # For loop to copy file to archive directory for DIRFILE in `find $dir -name "$name"|grep -v "^$dir$"|grep -v ".gz$"` do if [ "`dirname "$DIRFILE"|grep ^$DIR`" = "" ]; then DIR="$DIRFILE" let CNT=CNT+1 echo "cp -rp $DIRFILE $ARCHIVE_DIR/`basename $DIRFILE`.`basename $dir`.$CNT.`date '+%Y%m%d'`" cp -rp "$DIRFILE" "$ARCHIVE_DIR/`basename $DIRFILE`.`basename $dir`.$CNT.`date '+%Y%m%d'`" fi if [ $? -eq 0 ]; then if [ ! -d "$DIRFILE" ]; then echo "cat /dev/null > $DIRFILE" cat /dev/null > "$DIRFILE" fi else echo "[ALERT] `hostname`: Housekeep file $dir/$FILE failure ($0)" # mailx -r yourmail@company.com -s "[ALERT] `hostname`: Housekeep file $dir/$FILE failure ($0)" $RECEIVER_LINUX < $LOG fi find $ARCHIVE_DIR -name "`basename $DIRFILE`*" -mtime +$ziptime|grep -v ".gz$" >> $GZLIST find $ARCHIVE_DIR -name "`basename $DIRFILE`*gz" -mtime +$deltime >> $RMLIST done else # mailx -r yourmail@company.com -s "[ALERT] `hostname`: Housekeep directory $ARCHIVE_DIR does not exist ($0)" $RECEIVER_LINUX < $LOG echo "[ALERT] `hostname`: Housekeep directory $ARCHIVE_DIR does not exist ($0)" fi else find $dir -name "$name" -mtime +$ziptime|grep -v "^$dir$"|grep -v ".gz$" >> $GZLIST find $dir -name "$name.gz" -mtime +$deltime >> $RMLIST fi done DIR="[EMPTY]" if [ -s $GZLIST ]; then cat $GZLIST|sort -u|while read i do if [ -d "$i" ]; then DIR="$i" echo "gzip -r $i" gzip -r "$i" echo "mv $i $i.gz" mv "$i" "$i.gz" echo "$i zipped" else if [ "`dirname "$i"|grep ^$DIR`" = "" ]; then if [ -f "$i" ]; then echo "gzip $i" gzip "$i" echo "$i zipped" fi fi fi done rm $GZLIST fi if [ -s $RMLIST ]; then cat $RMLIST|sort -u|while read j do ls -ld "$j" if [ $? -eq 0 ]; then echo "rm -rf $j" rm -rf "$j" echo "$j removed" else echo "$j removed" fi done rm $RMLIST fi } > $LOG 2>&1 # ------------------------------- End of program -------------------------------
将脚本加入cronjob
### to housekeep OS log### 0 0 * * * /housekeep/scripts/housekeep_ux.sh > /dev/null 2>&1