logrotate
配置参数 参数说明 create mode owner group 轮转时指定创建新文件的属性,如 create 0777 nobody nobody daily 指定转储周期 – 每天 weekly 指定转储周期 – 每周 monthly 指定转储周期 – 每月 rotate 180 保留多少个日志文件(轮转几次)-- 默认保留 4 个 dateext 使用当前日期作为命名格式 – access.log-2020-09-20 dateformat -%Y-%m-%d-%s 配合 dateext 使用,必须紧跟 dateext 出现,只支持 %Y %m %d %s 四个参数 copytruncate 用于还在打开的日志文件,把当前日志备份并截断;先拷贝再清空,可能会丢失部分日志数据。 nocopytruncate 备份日志文件,但不截断 nocreate 不建立新的日志文件 compress 通过 gzip 压缩转储后的日志 delaycompress 转储的日志文件到下一次转储时才压缩 missingok 如果日志丢失,不报错继续滚动下一个日志 ifempty 文件为空同样转储 olddir /data/old_log 转储后存放目录,必须和当前日志文件在同一个文件系统 noolddir 转储后文件和当前日志文件放在同一个目录下 sharedscripts 运行 postrotate 脚本,在所有日志都轮转后统一执行一次脚本 prerotate/endscript logrotate 转储前 – 执行的命令 (endscript 结尾,必须独立成行) postrotate/endscript logrotate 转储前 – 执行的命令 (endscript 结尾,必须独立成行) size (或minsize) log-size 日志文件达到指定大小后进行转储,log-size 可以指定 bytes(缺省)、KB(sizek)、MB(sizem)
logrotate 配置查看
[ root@demo ~]
/usr/local/nginx/logs/*.log {
create 0644 root root
daily
rotate 180
dateext
dateformat -%Y-%m-%d-%s
copytruncate
compress
delaycompress
missingok
ifempty
noolddir
sharedscripts
postrotate
/bin/kill -USR1 ` cat /usr/local/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
endscript
}
查看 nginx 日志目录内容
[ root@prometheus ~]
total 12
-rw-r--r--. 1 root root 4202 Sep 24 18:03 access.log
-rw-r--r--. 1 root root 0 Sep 24 18:02 error.log
-rw-r--r--. 1 root root 6 Sep 24 15:40 nginx.pid
调试测试
[ root@prometheus ~]
reading config file /etc/logrotate.d/nginx
Allocating hash table for state file, size 15360 B
Handling 1 logs
rotating pattern: /usr/local/nginx/logs/*.log after 1 days ( 180 rotations)
empty log files are rotated, old logs are removed
considering log /usr/local/nginx/logs/access.log
log does not need rotating ( log has been already rotated)
considering log /usr/local/nginx/logs/error.log
log does not need rotating ( log has been already rotated)
not running prerotate script, since no logs will be rotated
手动执行 && 查看
执行转储
[ root@prometheus ~]
验证转储结果
[ root@prometheus ~]
total 16
-rw-r--r--. 1 root root 3820 Sep 24 18:04 access.log
-rw-r--r--. 1 root root 4202 Sep 24 18:03 access.log-2020-09-24-1600941822
-rw-r--r--. 1 root root 0 Sep 24 18:03 error.log
-rw-r--r--. 1 root root 0 Sep 24 18:03 error.log-2020-09-24-1600941822
-rw-r--r--. 1 root root 6 Sep 24 15:40 nginx.pid
配置计划任务 – 自动执行
计划任务配置
[ root@prometheus ~]
Thu Sep 24 18:06:22 CST 2020
[ root@prometheus ~]
10 18 * * * logrotate -f /etc/logrotate.d/nginx
计划任务查看
[ root@prometheus ~]
10 18 * * * logrotate -f /etc/logrotate.d/nginx
计划任务执行结果验证
[ root@prometheus ~]
Thu Sep 24 18:10:19 CST 2020
[ root@prometheus ~]
Sep 24 18:06:47 prometheus crontab[ 15270] : ( root) END EDIT ( root)
Sep 24 18:07:41 prometheus crontab[ 15273] : ( root) LIST ( root)
Sep 24 18:10:01 prometheus CROND[ 15288] : ( root) CMD ( logrotate -f /etc/logrotate.d/nginx)
[ root@prometheus ~]
total 20
-rw-r--r--. 1 root root 0 Sep 24 18:18 access.log
-rw-r--r--. 1 root root 233 Sep 24 18:03 access.log-2020-09-24-1600941822.gz
-rw-r--r--. 1 root root 4775 Sep 24 18:18 access.log-2020-09-24-1600942681
-rw-r--r--. 1 root root 0 Sep 24 18:18 error.log
-rw-r--r--. 1 root root 20 Sep 24 18:03 error.log-2020-09-24-1600941822.gz
-rw-r--r--. 1 root root 0 Sep 24 18:18 error.log-2020-09-24-1600942681
-rw-r--r--. 1 root root 6 Sep 24 15:40 nginx.pid
参考