本文共 1170 字,大约阅读时间需要 3 分钟。
nginx日志会随着访问量而变大,较大的日志会影响分析日志时对日志的分析和处理,我们可以使用shell对nginx日志进行定时分割,以周或者天,对于时间久的日志可以定时删除,减小磁盘的浪费
代码:
# cat logrotate.sh #!/bin/bash#rotate nginx logs#writen by AikerPATH=$PATH://usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binlogs_path=/var/log/nginxd=$(date -d "-1 day" +%F)mv $logs_path/access.log $logs_path/access_${d}.logmv $logs_path/error.log $logs_path/error_${d}.log#向nginx主进程发送USR1信号重新打开日志文件kill -USR1 $(cat /var/run/nginx.pid)
针对多个日志的切割:
#!/bin/bash#rotate nginx logs#writen by AikerPATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/binlogs_path=/var/log/nginxd=$(date -d "-1 day" +%F)cd ${logs_path}for logs in `ls *.log` do mv $logs $logs-$ddone#向nginx主进程发送USR1信号重新打开日志文件也可以:/bin/kill -HUP `cat $nginx_pid`kill -USR1 $(cat /var/run/nginx.pid)
定时切割日志:
crontab -e0 0 * * 0 /root/script/logrotate.sh > /dev/null 2>&1
# ll /var/log/nginx/ total 64-rw-r--r-- 1 nginx root 3205 Feb 12 11:42 access_2018-02-11.log-rw-r--r-- 1 nginx root 41951 Feb 12 11:53 access.log-rw-r--r-- 1 nginx root 14441 Feb 9 10:12 error_2018-02-11.log-rw-r--r-- 1 nginx root 0 Feb 12 11:43 error.log
转载于:https://blog.51cto.com/m51cto/2071304