logrotate是Linux系统自带,无需安装nginx
进入【/etc/logrotate.d/nginx】文件修改配置post
# 须要备份的日志路劲,一个或多个均可以 /data/logs/nginx1/*.log /data/logs/nginx2/*.log { daily rotate 5 missingok dateext compress notifempty sharedscripts postrotate [ -e /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid` endscript }
logrotate -vf /etc/logrotate.d/nginx
执行以上命令,是否获得本身预期效果测试
0 0 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginx
将上面这段添加到crontab里面,让他天天定时执行日志
配置 | 说明 |
---|---|
daily | 指定转储周期为天天 |
weekly | 指定转储周期为每周 |
monthly | 指定转储周期为每个月 |
rotate | 转储次数,超过将会删除最老的那一个 |
missingok | 忽略错误,如“日志文件没法找到”的错误提示 |
dateext | 切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期 |
compress | 经过gzip 压缩转储旧的日志 |
delaycompress | 当前转储的日志文件到下一次转储时才压缩 |
notifempty | 若是日志文件为空,不执行切割 |
sharedscripts | 只为整个日志组运行一次的脚本 |
prerotate/endscript | 在转储之前须要执行的命令能够放入这个对,这两个关键字必须单独成行 |
postrotate/endscript | 在转储之后须要执行的命令能够放入这个对,这两个关键字必须单独成行 |