如何使用 logrotate 命令保持日志文件更新

本文详细介绍Linux logrotate命令如何高效管理日志,包括轮转、压缩、邮件通知及配置文件实战,助你掌握关键设置并保持日志整洁

导读    使用此 Linux 命令保持日志文件更新。logrotate 实用程序在管理日志方面非常出色。它可以轮转日志、压缩日志、通过电子邮件发送日志、删除日志、归档日志,并在你需要时开始记录最新的。
日志非常适合找出应用程序在做什么或对可能的问题进行故障排除。几乎我们处理的每个应用程序都会生成日志,我们希望我们自己开发的应用程序也生成日志。日志越详细,我们拥有的信息就越多。但放任不管,日志可能会增长到无法管理的大小,反过来,它们可能会成为它们自己的问题。因此,最好将它们进行裁剪,保留我们需要的那些,并将其余的归档。

基本功能
logrotate 实用程序在管理日志方面非常出色。它可以轮转日志、压缩日志、通过电子邮件发送日志、删除日志、归档日志,并在你需要时开始记录最新的。

运行 logrotate 非常简单——只需要运行 logrotate -vs state-file config-file。在上面的命令中,v 选项开启详细模式,s 指定一个状态文件,最后的 config-file 是配置文件,你可以指定需要做什么。Linux就该这么学

实战演练
让我们看看在我们的系统上静默运行的 logrotate 配置,它管理我们在 /var/log 目录中找到的大量日志。查看该目录中的当前文件。你是否看到很多 *.[number].gz 文件?这就是 logrotate 正在做的。你可以在 /etc/logrotate.d/rsyslog 下找到此配置文件。我的配置文件如下:

/var/log/syslog
{
        rotate 7
        daily
        missingok
        notifempty
        delaycompress
        compress
        postrotate
                reload rsyslog > /dev/null 2>&1 || true
        endscript
}
 
/var/log/mail.info
/var/log/mail.warn
/var/log/mail.err
/var/log/mail.log
/var/log/daemon.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/lpr.log
/var/log/cron.log
/var/log/debug
/var/log/messages
 
{
        rotate 4
        weekly
        missingok
        notifempty
        compress
        delaycompress
        sharedscripts
        postrotate
                reload rsyslog > /dev/null 2>&1 || true
        endscript
}
该文件首先定义了轮转 /var/log/syslog 文件的说明,这些说明包含在后面的花括号中。以下是它们的含义:

rotate 7: 保留最近 7 次轮转的日志。然后开始删除超出的。
daily: 每天轮转日志,与 rotate 7一起使用,这意味着日志将保留过去 7 天。其它选项是每周、每月、每年。还有一个大小参数,如果日志文件的大小增加超过指定的限制(例如,大小 10k、大小 10M、大小 10G 等),则将轮转日志文件。如果未指定任何内容,日志将在运行 logrotate 时轮转。你甚至可以在 cron 中运行 logrotate 以便在更具体的时间间隔内使用它。
missingok:如果日志文件缺失也没关系。不要惊慌。
notifempty:日志文件为空时不轮转。
compress: 开启压缩,使用 nocompress 关闭它。
delaycompress:如果压缩已打开,则将压缩延迟到下一次轮转。这允许至少存在一个轮转但未压缩的文件。如果你希望昨天的日志保持未压缩以便进行故障排除,那么此配置会很有用。如果某些程序在重新启动/重新加载之前可能仍然写入旧文件,这也很有帮助,例如 Apache。
postrotate/endscript: 轮转后运行此部分中的脚本。有助于做清理工作。还有一个 prerotate/endscript 用于在轮转开始之前执行操作。
你能弄清楚下一节对上面配置中提到的所有文件做了什么吗?第二节中唯一多出的参数是 sharedscripts,它告诉 logrotate 在所有日志轮转完成之前不要运行 postrotate/endscript 中的部分。它可以防止脚本在每一次轮转时执行,只在最后一次轮转完成时执行。

看点新的东西
我使用下面的配置来处理我系统上的 Nginx 的访问和错误日志。

/var/log/nginx/access.log
/var/log/nginx/error.log  {
        size 1
        missingok
        notifempty
        create 544 www-data adm
        rotate 30
        compress
        delaycompress
        dateext
        dateformat -%Y-%m-%d-%s
        sharedscripts
        extension .log
        postrotate
                service nginx reload
        endscript
}
上面的脚本可以使用如下命令运行:

logrotate -vs state-file /tmp/logrotate
第一次运行该命令会给出以下输出:

reading config file /tmp/logrotate
extension is now .log
 
Handling 1 logs
 
rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log   1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log needs rotating
considering log /var/log/nginx/error.log
  log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508250'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508250.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx
第二次运行它:

reading config file /tmp/logrotate
extension is now .log
 
Handling 1 logs
 
rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log   1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log needs rotating
considering log /var/log/nginx/error.log
  log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508280'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508280.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx
第三次运行它:

reading config file /tmp/logrotate
extension is now .log
 
Handling 1 logs
 
rotating pattern: /var/log/nginx/access.log
/var/log/nginx/error.log   1 bytes (30 rotations)
empty log files are not rotated, old logs are removed
considering log /var/log/nginx/access.log
  log needs rotating
considering log /var/log/nginx/error.log
  log does not need rotating
rotating log /var/log/nginx/access.log, log->rotateCount is 30
Converted ' -%Y-%m-%d-%s' -> '-%Y-%m-%d-%s'
dateext suffix '-2021-08-27-1485508316'
glob pattern '-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
compressing log with: /bin/gzip
renaming /var/log/nginx/access.log to /var/log/nginx/access-2021-08-27-1485508316.log
creating new /var/log/nginx/access.log mode = 0544 uid = 33 gid = 4
running postrotate script
* Reloading nginx configuration nginx
状态文件的内容如下所示:

logrotate state -- version 2
"/var/log/nginx/error.log" 2021-08-27-9:0:0
"/var/log/nginx/access.log" 2021-08-27-9:11:56

### Linux 中配置 logrotate 来定期清理日志 #### 配置文件结构说明 Logrotate 是一个强大的日志管理工具,其核心功能通过配置文件实现。默认情况下,logrotate 的全局配置文件位于 `/etc/logrotate.conf`,而特定服务的日志轮转规则通常存储在 `/etc/logrotate.d/` 目录下。 以下是常见的配置选项及其含义: - `daily`: 表示每日执行一次日志轮转。 - `weekly`: 表示每周执行一次日志轮转。 - `monthly`: 表示每月执行一次日志轮转。 - `size <尺寸>`: 当日志文件达到指定大小时触发轮转。 - `rotate <数量>`: 设置保留的历史日志文件的数量。 - `compress`: 启用压缩已轮转的日志文件。 - `delaycompress`: 延迟一天再压缩日志文件(配合 compress 使用)。 - `missingok`: 如果日志文件不存在,则不会报错。 - `notifempty`: 如果日志文件为空,则不进行轮转。 - `create <权限> <用户> <组>`: 创建新日志文件使用的模式、用户和组。 - `sharedscripts`: 只有在所有日志文件都被处理完毕后才运行 postrotate 脚本。 - `postrotate ... endscript`: 在每次日志轮转完成后执行的命令或脚本。 #### 示例配置文件 以下是一个典型的 Nginx 日志轮转配置示例,假设日志路径为 `/var/log/nginx/*.log`: ```bash /var/log/nginx/*.log { daily # 每天轮转一次 missingok # 文件丢失时不报错 rotate 14 # 保留最近 14 天的日志 compress # 对旧日志启用 gzip 压缩 delaycompress # 推迟到第二天压缩 notifempty # 不会轮转空日志文件 create 0640 nginx adm # 新建日志文件的权限为 0640,属主为 nginx,属组为 adm sharedscripts # 所有日志均完成后再运行脚本 postrotate # 轮转后的操作 /bin/kill -USR1 $(cat /run/nginx.pid 2>/dev/null) 2>/dev/null || true endscript } ``` 此配置表示每天检查 Nginx 日志文件,并按设定条件进行轮转和清理[^2]。 #### 测试配置有效性 为了验证配置是否正确无误,可以使用 `-d` 参数模拟运行而不实际修改任何日志文件;或者使用 `-f` 参数强制立即执行一轮日志轮转。例如: ```bash /usr/sbin/logrotate -d /etc/logrotate.conf # 干运行调试模式 /usr/sbin/logrotate -f /etc/logrotate.conf # 强制执行日志轮转 ``` 如果希望查看详细的输出信息,还可以加上 `-v` 参数以获得更丰富的反馈[^3]。 #### 自动化调度机制 Logrotate 默认由系统的定时任务框架 Cron 控制执行频率。具体而言,在大多数发行版中,存在一个名为 `/etc/cron.daily/logrotate` 的脚本负责调用 logrotate 工具按照预设计划工作。这意味着只要 cron 服务正常运转,logrotate 就能按时生效[^4]。 ### 注意事项 尽管 logrotate 提供了灵活多样的定制能力,但在实际部署过程中仍需注意以下几点: - 确认目标日志路径准确无误; - 根据磁盘容量合理规划历史日志保存期限与压缩策略; - 若涉及第三方应用(如 Nginx 或 Apache),务必查阅官方文档确认兼容性以及推荐的最佳实践。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值