logrotate 实现 CentOS 7 下 nginx 日志切割

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/endscriptlogrotate 转储前 – 执行的命令 (endscript 结尾,必须独立成行)
postrotate/endscriptlogrotate 转储前 – 执行的命令 (endscript 结尾,必须独立成行)
size (或minsize) log-size日志文件达到指定大小后进行转储,log-size 可以指定 bytes(缺省)、KB(sizek)、MB(sizem)

logrotate 配置查看

[root@demo ~]# cat /etc/logrotate.d/nginx 
/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 ~]# ll /usr/local/nginx/logs/
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 ~]# logrotate -d /etc/logrotate.d/nginx 
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 ~]# logrotate -f /etc/logrotate.d/nginx 
验证转储结果
[root@prometheus ~]# ll /usr/local/nginx/logs/
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 ~]# date
Thu Sep 24 18:06:22 CST 2020
  • 计划任务配置
[root@prometheus ~]# crontab -e

10 18 * * * logrotate -f /etc/logrotate.d/nginx
计划任务查看
[root@prometheus ~]# crontab -l
10 18 * * * logrotate -f /etc/logrotate.d/nginx
计划任务执行结果验证
  • 查看当前时间
[root@prometheus ~]# date
Thu Sep 24 18:10:19 CST 2020
  • 查看计划任务执行记录
[root@prometheus ~]# tail -n3 /var/log/cron
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 ~]# ll /usr/local/nginx/logs/
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

参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值