nginx自动按天切割日志

Posted by jintang on 2015-02-23

前几天服务器报警磁盘不足,上服务器一看. 我的天,access.log积累的日志太多了

实现按天切割日志

1
2
3
4
5
6
7
8
vim /home/scripts/cut_access_daily.sh

#!/bin/bash
# This script run at 00:00
# The Nginx logs path
logs_path=”/home/www/logs/”
mkdir -p ${logs_path}$(date -d “yesterday” +”%Y”)/$(date -d “yesterday” +”%m”)/
mv ${logs_path}access.log ${logs_path}$(date -d “yesterday” +”%Y”)/$(date -d “yesterday” +”%m”)/access_$(date -d “yesterday” +”%Y%m%d”).log

再发USR1信号给nginx,重新启动日志

1
kill -USR1 `cat /usr/local/nginx/nginx.pid`

添加定时任务:

1
00 00 * * * /bin/bash /home/scripts/cut_nginx_daily.sh