1.需求
- nginx的日志文件路径
- 每天0点对nginx日志进行切割
- 以前一天的日期为命名
2.脚本
- date命令:
date -d 'yesterday' +%Y-%m-%d
#!/bin/bash
#Auto cut nginx log script.
#create by 小C
#create date:2019-08-21
#nginx日志路径
logs_path=/usr/local/nginx/logs
YesterDay=$(date -d 'yesterday' +%Y-%m-%d)
#移动日志并以日期改名
mv ${logs_path}/access.log ${logs_path}/access_${YesterDay}.log
mv ${logs_path}/error.log ${logs_path}/error_${YesterDay}.log
#向nginx主进程发送信号,重新生成日志文件
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
3.定时任务
crontab -e
0 0 * * * sh /root/nginx_cut.sh
crontab -l