1 编写脚本cut_nginx_log.sh,放置在nginx/sbin/目录下,修改成可执行
#!/bin/bash
# This script run at 00:00
#放置日志目录
logs_path="/var/www/logs"
#nginx 目录
nginx_path="/var/www/nginx"
#创建日期目录
mkdir -p ${logs_path}/$(date -d "yesterday" +"%Y.%m")/
#移动nginx/logs/access.log日志到指定目录
mv ${nginx_path}/logs/access.log ${logs_path}/$(date -d "yesterday" +"%Y.%m")/$(date -d "yesterday" +"%Y.%m.%d").log
#通知nginx进程刷新日志文件
kill -USR1 `cat ${nginx_path}/logs/nginx.pid`
2 添加系统定时任务:sudo crontab -e
00 00 * * * /var/www/nginx/sbin/cut_nginx_log.sh
时间: 2024-11-03 01:28:16