新建脚本文件
vim clearLog.sh
#!/bin/bash
# 定时删除队列日志文件 保留最近7天
limit_time=`date -d "7 days ago" +%s`
echo "清理" `date '+%Y-%m-%d %H:%M:%S' -d @$limit_time` "前的日志"
current_dir=$(cd $1; pwd)
searchfile() {
for chkfile in $1/*
do
if [[ -f $chkfile ]] && [[ ${chkfile##*.} = "log" ]]; then
last_modify_time=`stat -c %Y $chkfile`
if [ $limit_time -gt $last_modify_time ]; then
echo $chkfile
`rm -rf $chkfile`
fi
fi
if [[ -d $chkfile ]]; then
searchfile $chkfile
fi
done
}
searchfile $current_dir
执行
/bin/sh /path/clearLog.sh /path/ >> /path/clearLog.log 2>&1