nginx日志切割

系统:centos7

nginx:编译安装1.16.1

创建定时任务

[root@nginx1 shell]# crontab -l
0 1 * * * /root/shell/cut_log.sh

编写脚本

[root@nginx1 shell]# cat cut_log.sh
#!/bin/bash
#日志路径
logs_path="/usr/local/nginx/logs/"
#删除过期的文件
find $logs_path -mtime +30 -name "access_*.log" -exec rm -f {} \;
#获取nginx进程pid
nginx_pid="/usr/local/nginx/logs/nginx.pid"
/usr/bin/mv ${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
#重新打开日志文件
kill -USR1 `cat ${nginx_pid}`

查看效果

 

posted @ 2020-10-26 14:59  水中鱼2018  阅读(71)  评论(0)    收藏  举报