分类: Linux
Nginx按天或按小时切割日志

Nginx日志访问量大的时候,日志文件增长很快,查询日志很不方便,以下为实现按天或者小时将access文件切割:
按天切割方法如下:
创建log.sh脚本:

vim /usr/local/nginx/logs/log.sh
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
mv ${LOG_PATH}access.log ${LOG_PATH}access-${YESTERDAY}.log
mv ${LOG_PATH}error.log ${LOG_PATH}error-${YESTERDAY}.log
/usr/local/nginx/sbin/nginx -s reload

给权限

chmod +x /usr/local/nginx/logs/log.sh
crontal -e 加入定时执行,每天凌晨的时候,对日志进行切割,并reload

00 00 * * * /usr/local/nginx/logs/log.sh

按小时切割:
创建log.sh脚本:

vim /usr/local/nginx/logs/log.sh
#!/bin/bash
LOG_PATH=/usr/local/nginx/logs/
YESTERDAY=$(date -d "-1hours" +%Y-%m-%d_%H)
mv ${LOG_PATH}access.log ${LOG_PATH}access-${YESTERDAY}.log
mv ${LOG_PATH}error.log ${LOG_PATH}error-${YESTERDAY}.log
/usr/local/nginx/sbin/nginx -s reload
给权限

chmod +x /usr/local/nginx/logs/log.sh
crontal -e 加入定时执行,每个整点的时候,对日志进行切割,并reload

*/60  * * * * /usr/local/nginx/logs/log.sh


相关博文:

发表新评论