使用定时任务+脚本方式对nginx进行日志切分
nginx路径:/home/nf/nginx
nginx日志路径:/home/nf/nginx/logs
切分目标文件:access.log error.log
创建脚本:
1 vim /home/nf/cut_nginx_log.sh
1 #!/bin/bash 2 #nginx log path 3 LOGS_PATH=/home/nf/nginx/logs 4 YESTERDAY=$(date -d "yesterday" +%Y-%m-%d) 5 KEEPTIME=$(date -d "-30 days" +%Y-%m-%d) 6 #cut logs 7 mv ${LOGS_PATH}/access.log ${LOGS_PATH}/date/access_${YESTERDAY}.log 8 mv ${LOGS_PATH}/error.log ${LOGS_PATH}/date/error_${YESTERDAY}.log 9 10 kill -USR1 $(cat /home/nf/nginx/logs/nginx.pid) 11 12 #remove 30 days before logs 13 rm -f ${LOGS_PATH}/date/access_{KEEPTIME}.log 14 rm -f ${LOGS_PATH}/date/error_{KEEPTIME}.log
(脚本中使用KEEPTIME变量,取得30天之前的日期,做删除用;
USR1用作通知应用重载配置文件;)
在nginx的log中新建文件夹
1 mkdir /home/nf/nginx/logs/date
创建crontab定时任务
1 crontab -e
1 0 0 * * * sh /home/nf/cut_nginx_log.sh
(使定时任务每天0点执行,将前一天的access.log和error.log文件改为带昨日日期时间戳的log文件,然后重载配置文件,生成新的access.log,error.log存储新一天的记录)
1 crontab -l
标签:log,切分,nf,nginx,PATH,home,日志,LOGS From: https://www.cnblogs.com/danielmiau/p/17457872.html