#!/bin/bash
#nginx logs cut
#cp /usr/local/nginx/logs/access.log /usr/local/nginx/logs/access.log.bak
PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH
log_path="/usr/local/nginx/logs/"
log_move="/var/log/nginx/"
log_name="access.log"
nginx_pid=`ps aux | grep 'nginx: master' | grep -v 'grep nginx' | awk '{print $2}'`
#cut logs every day.
mkdir -p ${log_move}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${log_path}${log_name} ${log_move}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 $nginx_pid

现在的nginx日志存放目录 log_path="/usr/local/nginx/logs/"

切割后的日志存放目录log_move="/var/log/nginx/"


加入crontab 定时任务

0 0 * * * sh /root/nginxlogcut.sh

nginx日志切割shell脚本_第1张图片

参考http://blog.s135.com/nginx_php_v6/