Logrotate管理日志

Logrotate是一个日志文件管理工具。用来把旧文件轮转、压缩、删除,并且创建新的日志文件。我们可以根据日志文件的大小、天数等来转储,便于对日志文件管理,一般都是通过cron计划任务来完成的。

文件位置

  • 默认状态文件在/var/lib/logrotate.status
  • 默认配置文件是/etc/logrotate.conf

运行原理

Logrotate是基于CRON来运行的,其脚本在/etc/cron.daily/logrotate

实际运行时,Logrotate会调用配置文件/etc/logrotate.conf

这里的设置是global attribute,而在/etc/logrotate.d目录里,可以定义每项应用服务的配置文件,并且定义会覆盖当下。

定时执行/etc/cron.daily目录下的文件的设置,则在/etc/anacrontab里定义的

命令参数

Usage: logrotate [OPTION...] <configfile>
  -d, --debug               Don't do anything, just test (implies -v)
  -f, --force               Force file rotation
  -m, --mail=command        Command to send mail (instead of `/bin/mail')
  -s, --state=statefile     Path of state file
  -v, --verbose             Display messages during rotation
  --version                 Display version information

Help options:
  -?, --help                Show this help message
  --usage                   Display brief usage message

配置参数

compress 使用gzip来压缩日志文件

nocompress 日志不压缩的参数

compresscmd 指定压缩工具,默认gzip

uncompresscmd 指定解压工具,默认gunzip

delaycompress 推迟要压缩的文件,直到下一轮询周期再执行压缩,可与compress搭配使用

dateext 轮询的文件名字带有日期信息

dateformat 格式化归档日期后缀,只有%Y, %m,  %d 和%s


daily 日志按天轮询

weekly 日志按周轮询

monthly 日志按月轮询

yearly 日志按年轮询日志,不关注


maxage count 删除count天前的轮询日志文件

rotate count 删除count个外的轮询日志文件

notifempty 文件为空时,不进行轮询

size size 日志文件根据大小规定进行轮询,默认单位是byte,也可指定kb, M, G;So size 100, size 100k, size 100M and size 100G are all valid.


minsize size 文件大小超过size后才能进行轮询,此时会略过时间参数

postrotate/endscript 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。

sharedscripts 在所有的日志文件都轮询完毕后统一执行一次脚本。当声明日志文件的时候使用通配符时需要用到

olddir 指定轮询后的日志文件存放在directory,必须和当前日志文件在同一个文件系统

create mode owner group 新日志文件的权限,属主属组

 

实际情况下,还得通知应用服务重载配置,一般会在最后加上重启命令,以Nginx为例:

 postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
  endscript

USR1亦通常被用来告知应用程序重载配置文件;例如,向Apache HTTP服务器发送一个USR1信号将导致以下步骤的发生:停止接受新的连接,等待当前连接停止,重新载入配置文件,重新打开日志文件,重启服务器,从而实现相对平滑的不关机的更改。

https://www.jianshu.com/p/6d3647f02437

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值