1、创建脚本 cut_nginx_log.sh
#!/bin/sh# This script run at 00:00
# The Nginx logs pathlogs_path="/usr/local/nginx/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
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
2、设置crontab,每天凌晨00:00切割nginx访问日志
crontab -e
内容如下:
00 00 * * * sh /usr/local/nginx/cut_nginx_log.sh
3、测试:运行
转移的log如下
[root@~ logs]# ll 2011/08/access_20110808.log
-rw-r--r-- 1 www root 0 Aug 9 10:18 2011/08/access_20110808.log
保留180份(也就是删除180天以前的),在上述脚本加入
DIR=/usr/local/nginx/logs/`/bin/date +%Y/%m/%d --date '180 days ago'`
/bin/rm -rf ${DIR}/*