Linux系统之logrotate的基本使用

举报
江湖有缘 发表于 2025/03/31 16:06:26 2025/03/31
296 0 0
【摘要】 Linux系统之logrotate的基本使用

一、工具简介

logrotate是一个在Unix/Linux系统中用来管理日志文件的工具,它可以定期地轮换、压缩、删除日志文件,以避免日志文件过大而占用过多磁盘空间。通过配置logrotate,可以实现对各种类型日志文件的自动管理,确保系统日志的可靠性和保留合理的存储空间。

logrotate是Linux系统自带的日志管理工具,通过自动化实现:

  • 日志轮转:按时间/大小切割日志文件
  • 压缩归档:使用gzip/bzip2等压缩历史日志
  • 清理策略:自动删除过期日志文件
  • 权限管理:保持文件属主和权限一致性

二、核心工作机制

  1. 通过cron定时任务每日触发
  2. 读取/etc/logrotate.conf主配置文件
  3. 加载/etc/logrotate.d/目录下的自定义配置
  4. 执行日志切割策略(创建新文件/压缩/删除等)
  5. 发送HUP信号或执行自定义命令通知服务刷新

三、环境准备(安装验证)

# 检查系统是否已安装
which logrotate

# CentOS/RHEL安装
sudo yum install logrotate -y

# Ubuntu/Debian安装
sudo apt-get install logrotate -y

# 验证版本(推荐使用3.9+)
logrotate --version
[root@openEuler ~]# logrotate --version
logrotate 3.21.0

    Default mail command:       /bin/mail
    Default compress command:   /bin/gzip
    Default uncompress command: /bin/gunzip
    Default compress extension: .gz
    Default state file path:    /var/lib/logrotate/logrotate.status
    ACL support:                yes
    SELinux support:            yes

四、配置文件详解

4.1 检查Nginx服务状态

  • 提前安装Nginx服务,检查Nginx服务状态。
 yum -y install  nginx
 systemctl enable --now nginx
[root@openEuler ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
     Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: disabled)
     Active: active (running) since Mon 2025-03-31 15:34:50 CST; 14s ago
    Process: 4435 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
    Process: 4438 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
    Process: 4441 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
   Main PID: 4442 (nginx)
      Tasks: 5 (limit: 47332)
     Memory: 12.7M ()
     CGroup: /system.slice/nginx.service
             ├─4442 "nginx: master process /usr/sbin/nginx"
             ├─4443 "nginx: worker process"
             ├─4444 "nginx: worker process"
             ├─4445 "nginx: worker process"
             └─4447 "nginx: worker process"

Mar 31 15:34:50 openEuler systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 31 15:34:50 openEuler nginx[4438]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 31 15:34:50 openEuler nginx[4438]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 31 15:34:50 openEuler systemd[1]: Started The nginx HTTP and reverse proxy server.

4.2 主配置文件结构

# /etc/logrotate.conf

weekly          # 默认每周执行
rotate 4        # 保留4个历史版本
create          # 轮转后创建新文件
dateext         # 使用日期作为后缀
compress        # 启用gzip压缩
include /etc/logrotate.d  # 包含子配置

4.3 自定义配置示例(以Nginx日志为例)

vim /etc/logrotate.conf

然后添加以下内容(根据你的需求调整):

/var/log/nginx/*.log {
    daily                   # 每天进行一次日志轮转
    missingok               # 如果日志文件丢失,不报错并继续处理其他日志文件
    rotate 30               # 保留最近30个归档日志
    compress                # 使用gzip压缩旧日志文件
    delaycompress           # 延迟压缩,即最新的归档日志不会被压缩
    notifempty              # 如果日志为空,则不轮转
    create 0640 www-data adm # 创建新的日志文件权限和用户组
    sharedscripts           # 共享脚本,在所有日志轮转后只运行一次脚本
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript               # 在日志轮转后执行该脚本,重新加载Nginx服务
}

其中一些关键选项解释如下:

  • daily: 设置每天轮转。
  • rotate 30: 设置保留最近30个归档日志。
  • compress: 启用日志文件压缩。
  • delaycompress: 推迟压缩直到下一次轮转。
  • notifempty: 如果日志为空则不进行轮转。
  • create: 设置新日志文件的权限及所属用户和组。
  • sharedscripts: 确保在所有日志轮转完成后仅执行一次 postrotate 脚本。
  • postrotate ... endscript: 在轮转之后执行的命令,这里是让 Nginx 重新打开日志文件。

4.4 测试配置

为了确保配置无误,可以在强制模式下运行 logrotate 来测试配置是否正确:

logrotate -d /etc/logrotate.d/nginx

这将进入调试模式,输出操作而不实际改变任何文件。若要真正执行轮转,请移除 -d 参数并添加 -f 强制执行标志:

logrotate -vf /etc/logrotate.d/nginx

在这里插入图片描述

4.5 设置定时任务

通常情况下,logrotate 会通过系统的定时任务(cron job)自动执行。检查 /etc/cron.daily/logrotate 或者 /etc/anacrontab 中的相关条目以确认调度情况。如果你需要自定义调度时间,可以编辑 crontab 文件:

crontab -e

添加一行如下的定时任务来每天凌晨执行 logrotate

0 0 * * * /usr/sbin/logrotate /etc/logrotate.conf

五、注意事项

  1. 确保配置文件语法正确,避免因错误的配置导致日志轮转失败。
  2. 定期检查日志轮转状态,确保日志文件按预期被轮转、压缩或删除。
  3. 在修改配置后,使用-d选项进行调试以模拟运行而不实际执行操作,确认配置无误。
  4. 使用-v选项可以查看详细的执行过程,有助于了解logrotate的行为和排查问题。
  5. 注意日志文件的权限设置,确保logrotate进程有权限读取旧日志并创建新日志文件。
  6. 若应用程序需要在日志轮转后重新打开日志文件,请正确配置postrotate脚本发送相应的信号给应用程序。
  7. 考虑使用copytruncate选项来处理不支持信号通知的应用程序,但要注意可能的数据丢失风险。
  8. 根据磁盘空间和日志重要性合理设置保留的日志文件数量或时间周期,避免不必要的存储消耗。
  9. 对于关键日志文件,在执行日志轮转前应先备份,以防意外情况导致数据不可恢复。
  10. 如果手动执行logrotate命令,记得移除强制标志(-f)以遵循正常的轮转条件,除非确实需要强制轮转。
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。