1.根据自己NGINX配置的日志存储路径编写脚本
#!/bin/bash
year=`date +%Y`
month=`date +%m`
day=`date +%d`
logs_backup_path="/data/logs/logs_backup/$year$month" #日志存储路径
logs_path="/data/logs/" #要切割的日志路径
logs_access="access" #要切割的日志
logs_error="error"
pid_path="/var/run/nginx.pid" #nginx的pid
[ -d $logs_backup_path ]||mkdir -p $logs_backup_path
rq=`date +%Y%m%d`
for i in `ls -al ${logs_path} | grep "^-" | awk '{print $9}' `
do
#echo ${i}
mv ${logs_path}${i} ${logs_backup_path}/${i}_${rq}.bak
done
#mv ${logs_path}${logs_access}.log ${logs_backup_path}/${logs_access}_${rq}.log
#mv ${logs_path}${logs_error}.log ${logs_backup_path}/${logs_error}_${rq}.log
kill -USR1 $(cat /var/run/nginx.pid)
2.设置定时任务
crontab –e
59 23 * * * bash /usr/local/nginx/shell/cut_ngnix_log.sh #每天23:59分开始执行;