Apache配置实现日志按天分割并删除指定几天前的日志
Apache日志默认情况下是一周切割一次,由于访问量大的时候日志的文件还是比较大的,同时也不利于管理员对日志的分析处理。于是尝试对Apache日志设置按天分割,并通过计划任务执行删除几天的日志。
在/etc/httpd/conf/httpd.conf配置
ErrorLog
"| /usr/sbin/rotatelogs /var/log/httpd/error_log-%Y%m%d 86400 480"
CustomLog
"| /usr/sbin/rotatelogs /var/log/httpd/access_log-%Y%m%d 86400 480"
common
使用命令rotatelogs 对日志进行切割,查找该命令的位置使用:which rotatelogs
指定日志文件的位置和名称/var/log/httpd/error_log-%Y%m%d
指定分割时间:86400 默认单位为s。也就是24小时
指定分区时差:480 默认单位m,也就是8小时
日志清理脚本:
#!/bin/bash
###########################################################
#Author:qingbo.song #
#Date:2016.01.29 #
#E-mail:qingbo.song@apicloud.com #
#Comment:http_clear_logs.sh #
#Path:/opt/shell #
#Crontab: 0 3 * * * sh /opt/shell/http_clear_logs.sh #
#Vesion:v1.0 #
###########################################################
#the date 7 days ago
Date_7=`
date
-d
"-2 day"
+%Y%m%d`
http_log_path=
"/var/log/httpd/"
#clear logs
rm
-fr ${http_log_path}
/access_log-
${Date_7}
rm
-fr ${http_log_path}
/error_log-
${Date_7}