cat >/etc/logrotate.d/nginx <<EOFnginx
/application/nginx/logs/access_bbs.log /application/nginx/logs/access_www.log { daily rotate 30 missingok dateext compress delaycompress notifempty sharedscripts postrotate if [ -f /application/nginx/logs/nginx.pid ]; then kill -USR1 `cat /application/nginx/logs/nginx.pid` fi endscript }
EOFbash
执行测试app
确保只剩下nginx本身的日志 以避免影响结果ide
[root@moban ~]# ls /application/nginx/logs/ -lh total 784M -rw-r--r-- 1 www root 9.0M Sep 12 15:45 access_bbs.log #<--日志文件不能为空 不然不能切割 -rw-r--r-- 1 root root 25K Sep 8 15:30 access.log -rw-r--r-- 1 www root 9.0M Sep 12 15:44 access_www.log #<--日志文件不能为空 不然不能切割 -rw-r--r-- 1 www root 766M Sep 8 19:17 error.log -rw-r--r-- 1 root root 5 Sep 12 15:27 nginx.pid
执行一下命令测试post
logrotate -vf /etc/logrotate.d/nginx测试
检查是否生成了日志,检查发现以日期形式生成了日志,说明切割成功spa
[root@moban ~]# ls /application/nginx/logs/ -lh total 784M -rw-r--r-- 1 www root 0 Sep 12 15:46 access_bbs.log -rw-r--r-- 1 www root 9.0M Sep 12 15:45 access_bbs.log-20180912 #切割生成了新日志 -rw-r--r-- 1 root root 25K Sep 8 15:30 access.log -rw-r--r-- 1 www root 0 Sep 12 15:46 access_www.log -rw-r--r-- 1 www root 9.0M Sep 12 15:44 access_www.log-20180912 #切割生成了新日志 -rw-r--r-- 1 www root 766M Sep 8 19:17 error.log -rw-r--r-- 1 root root 5 Sep 12 15:27 nginx.pid
添加定时任务日志
59 23 * * * /usr/sbin/logrotate -vf /etc/logrotate.d/nginxip
配置说明it
配置 |
说明 |
daily |
指定转储周期为天天 |
weekly |
指定转储周期为每周 |
monthly |
指定转储周期为每个月 |
rotate |
转储次数,超过将会删除最老的那一个 |
missingok |
忽略错误,如“日志文件没法找到”的错误提示 |
dateext |
切换后的日志文件会附加上一个短横线和YYYYMMDD格式的日期 |
compress |
经过gzip 压缩转储旧的日志 |
delaycompress |
当前转储的日志文件到下一次转储时才压缩 |
notifempty |
若是日志文件为空,不执行切割 |
sharedscripts |
只为整个日志组运行一次的脚本 |
prerotate/endscript |
在转储之前须要执行的命令能够放入这个对,这两个关键字必须单独成行 |
postrotate/endscript |
在转储之后须要执行的命令能够放入这个对,这两个关键字必须单独成行 |