当前位置: 技术问答>linux和unix
请大牛帮忙解答shell的意思
来源: 互联网 发布时间:2017-05-07
本文导语: 首先是crontab的意思 0 3 1 * * /root/scripts/sweep_apache_log > /root/logs/sweep_apache_log.log 2>&1 然后是sweep_apache_log的内容了 # define saving days SAVING_DAYS=90 # define log directory LOG_DIR=/var/log/httpd ACCESS_PREFIX='access.' ERROR_PREFIX='...
首先是crontab的意思
然后是sweep_apache_log的内容了
我大概知道这个是来删除过期的log文件的,可是具体就不知道是怎么一个流程了,希望大牛帮忙解答
万分感谢!
0 3 1 * * /root/scripts/sweep_apache_log > /root/logs/sweep_apache_log.log 2>&1
然后是sweep_apache_log的内容了
# 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
lastmonth=`date +%Y%m -d '2 months ago'`
for prefix in $ACCESS_PREFIX $ERROR_PREFIX ; do
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
我大概知道这个是来删除过期的log文件的,可是具体就不知道是怎么一个流程了,希望大牛帮忙解答
万分感谢!
|
# 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
|
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天的日志