apache日志清理策略

xiaoxiao2021-02-28  35

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
转载请注明原文地址: https://www.6miu.com/read-1400083.html

最新回复(0)