apache日志清理策略

apache日志文件清理策略:
1. 删除 apache 服务器已产生的日志文件信息
2.apache安装文件的配置文件修改,重新生成日志规则
       a. 打开apache 安装目录下的配置文件 /home/apache/apache2231/conf/httpd.conf
       b. ErrorLog logs/error_log
                   CustomLog logs/access_log common  两行注释掉。
       c. 新增两行(按照apache 自带日志轮循程序机制"rotatelogs" 每天生成日志)
ErrorLog "|/home/apache/apache2231/bin/rotatelogs /home/apache/apache2231/logs/error-%Y_%m_%d.log 86400 480"
CustomLog "|/home/apache/apache2231/bin/rotatelogs /home/apache/apache2231/logs/access-%Y_%m_%d.log 86400 480" common   
  d.配置文件修改完成,重启apache服务器: /home/apache/apache2231/bin/apachectl restart
3设置定时任务,定期清理日志文件
       a. 创建文件夹 mkdir -p /usr/local/crontab
                                 cd /usr/local/crontab
                                 touch clear_apache_logs.sh
 vi clear_apache_logs.sh 输入脚本:
   #! /bin/bash
         logdir=/home/apache/apache2231/logs
         pushd "$logdir"
         {
    filesum_access=$(ls access-*.log | wc -l)
    if [ $filesum_access -gt 7 ]; then
           delnum_access=$(($filesum_access - 7))
           rm -f $(ls -tr access-*.log | head -n $delnum_access)
    fi
    filesum_error=$(ls error-*.log | wc -l)
    if [ $filesum_error -gt 7 ]; then
           delnum_error=$(($filesum_error - 7))
           rm -f $(ls -tr error-*.log | head -n $delnum_error)
    fi
         }
         popd
b. 修改脚本执行权限 chmod 755 /usr/local/crontab/clear_apache_logs.sh
c. 建立定时任务( 每周星期天凌晨一点半执行日志清理)
       1 )查看 crontab 状态: /etc/init.d/crond status
       2 )如果 crontab 是启动状态,输入: crontab -e 进入编辑界面
       3 )输入定时任务: 30 1 * * 0 /usr/local/crontab/clear_apache_logs.sh
       4 )查看当期用户下的定时任务 crontab -l
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值