1.脚本制作
版主的nginx安装在/opt/nginx/nginx-1.15.1下
在/opt/nginx/nginx-1.15.1/logs目录下创建log.sh脚本
# /bin/bash
# 日志保存位置
base_path='/opt/nginx/nginx-1.15.1/logs'
# 获取当前年信息和月信息
log_path=$(date -d yesterday +"%Y%m")
# 获取昨天的日信息
day=$(date -d yesterday +"%d")
# 按年月创建文件夹
mkdir -p $base_path/$log_path
# 备份昨天的日志到当月文件夹
mv $base_path/access.log $base_path/$log_path/access_$day.log
mv $base_path/error.log $base_path/$log_path/error_$day.log
# 输出备份日志文件名
# echo $base_path/$log_path/access_$day.log
# 通过nginx信号量控制重读日志
kill -USR1 `cat /opt/nginx/nginx-1.15.1/logs/nginx.pid`
2.定时任务调用
vim /etc/crontab
0 0 * * * root /opt/nginx/nginx-1.15.1/logs/log.sh
定时每天00:00以root身份执行脚本/usr/local/nginx/nginx-1.15.1/logs/log.sh,实现定时自动分割Nginx日志,效果如下: