读书人

请大牛帮忙解答shell的意义

发布时间: 2014-01-17 00:06:00 作者: rapoo

请大牛帮忙解答shell的意思
首先是crontab的意思

                                                                            
[解决办法]

0 3 1 * * /root/scripts/sweep_apache_log > /root/logs/sweep_apache_log.log 2>&1

crond进程会在每个月的1号凌晨3点执行sweep_apache_log

sweep_apache_log脚本前面部分是对超过2个月的访问日志和错误日志使用gzip打包,最后部分是删除打包超过90天的日志
[解决办法]
# define saving days
SAVING_DAYS=90

# define log directory
LOG_DIR=/var/log/httpd
ACCESS_PREFIX='access.'
ERROR_PREFIX='error.'
LOG_SUFFIX='.log'

# compose archive file name
# 获取两个月之前的月份,比如 201311
lastmonth=`date +%Y%m -d '2 months ago'`

# 处理 Apache 的 access log 和 error log
for prefix in $ACCESS_PREFIX $ERROR_PREFIX ; do
# 设置归档日志的名字,比如 access201311.log
arcfile=${LOG_DIR}/${prefix}${lastmonth}.log

# already archived?
if [ -f ${arcfile} ]; then
# 对于已经做过归档的日志,直接进行压缩
gzip -9 ${arcfile}
echo 'logs are already archived, but they are not compressed'
continue
fi
if [ -f ${arcfile}.gz ]; then
echo 'logs are already sweeped'


continue
fi

# ok, trying to cat them and then gzip it
cat ${LOG_DIR}/${prefix}${lastmonth}*${LOG_SUFFIX} > ${arcfile}
gzip -9 ${arcfile}

# delete individual logs
rm -f ${LOG_DIR}/${prefix}${lastmonth}*${LOG_SUFFIX}

# end of for
done

# delete old archive files
find ${LOG_DIR} -type f -name '*.gz' -mtime +${SAVING_DAYS} -exec rm -f {} \;

# EOF

读书人网 >UNIXLINUX

热点推荐