前言
随着nginx访问量的增加,nginx日志 access.log 会越来越大
而已这样查看和管理日志文件都不方便
接下来就要把日志按天切割,生成一个新的日志文件,这样管理和查看会方便很多
然后做一个定时任务,让定时任务去自动执行这些操作
shell脚本实现
[root@Mike-VM-Node-10_10_0_188 ~]# vim /etc/init.d/nginx-log.sh #!/bin/bash base_path='/usr/local/nginx/logs/access.log' log_path='/usr/local/nginx/logs' log_day=$(date -d yesterday +"%d") mv $log_path/access.log $log_path_$log_day.log kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` [root@Mike-VM-Node-10_10_0_188 ~]# chmod +x /etc/init.d/nginx-log.sh
定时任务
[root@Mike-VM-Node-10_10_0_188 ~]# crontab -e * 0 * * * sh /etc/init.d/nginx-log.sh [root@Mike-VM-Node-10_10_0_188 ~]#
每天0点执行切割日志
清理30天之前的旧日志
[root@Mike-VM-Node-10_10_0_188 ~]# vim /etc/init.d/nginx-rmlog.sh #!/bin/bash find /usr/local/nginx/logs/ -mtime +30 -name "*.log" -exec rm -f {} ; [root@Mike-VM-Node-10_10_0_188 ~]# chmod +x /etc/init.d/nginx-rmlog.sh
定时任务
[root@Mike-VM-Node-10_10_0_188 ~]# crontab -e * * * * 5 sh /etc/init.d/nginx-rmlog.sh [root@Mike-VM-Node-10_10_0_188 ~]#
每周五执行一次清理30天的日志文件
本文分享完毕,感谢支持点赞~~