Linux 中的 logrotate 命令及示例 - Linux 命令行教程Linux 中的 logrotate 命令及示例 - Linux 命令行教程Linux 中的 logrotate 命令及示例 - Linux 命令行教程Linux 中的 logrotate 命令及示例 - Linux 命令行教程
  • 业务
  • 目标
  • 支持
  • 登录
找到的结果: {phrase} (显示: {results_count} 共: {results_count_total})
显示: {results_count} 共: {results_count_total}

加载更多搜索结果...

搜索范围
模糊匹配
搜索标题
搜索内容

Linux 中的 logrotate 命令及示例 - Linux 命令行教程

发表 admin at 2025年2月28日
类别
  • 未分类
标签

在Linux中,许多应用程序和系统服务都会存储日志文件。这些日志文件使 Linux 管理员能够深入了解其系统的运行情况,并且在解决问题时非常有用。然而,日志文件很快就会变得难以处理。例如,如果您的网络服务器软件记录对您网站的每次访问,并且每天有数千名查看者,那么将有太多信息无法挤入一个文本文件中。

这就是 Linux 中的 logrotate 命令发挥作用的地方。 logrotate 将定期获取当前日志文件,重命名它们,可选地压缩它们,并生成一个新文件,应用程序可以继续向该文件发送其日志。 logrotate 命令是从 cron 自动调用的,大多数服务都有自己的日志轮换配置,该配置在安装时实施。此配置告诉 logrotate 它应该如何处理旧日志文件。例如,在删除之前应该保留多少文件,是否应该压缩文件等。

系统管理员也可以使用logrotate实用程序来满足自己的需要。例如,如果 Linux 管理员设置要运行的脚本,并让该脚本定期生成日志,则可以设置 logrotate 来为我们管理日志文件。在本教程中,您将了解有关 logrotate 实用程序的更多信息,因为我们将通过配置它来轮换我们实现的服务的日志的示例。

在本教程中您将学习:

  • 如何在 Linux 上使用 logrotate 命令

  • logrotate配置文件的存储位置

  • 如何设置自定义 logrotate 配置

  • 如何测试 logrotate 实现

常用选项

对于希望控制 /var/log 目录的系统管理员来说,logrotate 是一个方便的工具。 logrotate 命令每天由 cron 调度程序调用,并读取以下文件:

  • logrotate配置文件/etc/logrotate.conf

  • logrotate配置目录下的文件 /etc/logrotate.d

系统上安装的大多数服务(Apache Webserver、postgreSQL、MySql、KDE 桌面管理器等)都会在 /etc/logrotate.d 中创建 logrotate 的配置文件。例如,以下是我们系统的 /etc/logrotate.d 目录的内容:

Linux 中的 logrotate 命令基本示例

假设我们正在运行一个名为“linuxserver”的服务,该服务正在 /var/log/linuxserver 目录中创建名为“linux.log”的日志文件。要将“linuxserver”日志文件包含在日志轮换中,我们需要首先创建一个 logrotate 配置文件,然后将其复制到 /etc/logrotate.d 目录中。

logrotate 配置文件看起来像这样:

/var/log/linuxserver/linux.log {
        rotate 7
        daily
        compress
        delaycompress
        missingok
        notifempty
        create 660 linuxuser linuxuser }

该配置文件将每天运行,创建最多 7 个由 linuxuser 和具有 660 权限的 linuxuser 组拥有的存档,压缩所有日志并仅排除昨天和空日志文件。以下是一些选定的 logrotate 配置关键字。有关完整的教程,请查看 logrotate 手册页。

daily Log files are rotated every day.
weekly Log files are rotated if the current weekday is less than the weekday of the last rotation or if more than a week has passed since the last rotation. This is normally the same as rotating logs on the first day of the week, but if logrotate is not being run every night a log rotation will happen at the first valid opportunity.
monthly Log files are rotated the first time logrotate is run in a month (this is normally on the first day of the month).
notifempty Do not rotate the log if it is empty (this overrides the ifempty option).
nocompress Old versions of log files are not compressed.
delaycompress Postpone compression of the previous log file to the next rotation cycle. This only has effect when used in combination with compress. It can be used when some program cannot be told to close its logfile and thus might continue writing to the previous log file for some time.
compress Old versions of log files are compressed with gzip by default.
mail address When a log is rotated out of existence, it is mailed to address. If no mail should be generated by a particular log, the nomail directive may be used.
missingok If the log file is missing, go on to the next one without issuing an error message.

实施logrotate配置文件

配置文件准备好后,只需将其复制到 logrotate 目录并更改所有者和权限:

# cp linuxserver /etc/logrotate.d/
# chmod 644 /etc/logrotate.d/linuxserver
# chown root.root /etc/logrotate.d/linuxserver

注意
您始终可以使用 man 命令来阅读有关 logrotate 命令及其官方文档的更多信息。单击上一个链接可查看如何打开 Linux 系统上任何命令的手册页。

高级用法

一旦实施了 logrotate 配置,系统上的 cron 调度程序将自动调用该命令,以便 logrotate 可以完成其工作。通常,用户不需要手动执行logrotate命令。但是,logrotate 命令仍然可以在终端中手动运行,并且有一些不同的选项可供使用。大多数情况下,这只是归结为测试您已实现的配置。检查下面的示例以了解其工作原理。

Linux 高级示例中的 logrotate 命令

  1. 您可以通过在没有任何要轮换的日志文件的情况下强制执行logrotate来尝试日志轮换(在通常的 cron 作业之外)。为此,请使用 -f 选项并指定您要使用的配置文件。

    # logrotate -f /etc/logrotate.d/linuxserver 
    
  2. 如果您遇到任何问题并希望进行调试,可以将 -d 选项与 logrotate 结合使用。这将模拟“测试运行”,但实际上不会进行任何更改。相反,它只会输出调试消息以帮助排除故障。

    # logrotate -d /etc/logrotate.d/linuxserver 
    
  3. 使用 -v 选项打开详细程度。这将在旋转期间显示消息,以便您可以准确地看到发生了什么。

    # logrotate -v /etc/logrotate.d/linuxserver 
    

结束语

在本教程中,我们了解了 Linux 中的 logrotate 命令,以及如何使用它来控制 /var/log 目录的文件。我们还了解了如何设置 logrotate 配置来管理我们实现的自定义服务的日志文件。虽然 logrotate 主要是一个在后台运行的命令,在 cron 中默默调用,但系统管理员可以利用它的功能来使自己的日志文件更有条理、更易于管理。

©2015-2025 Norria support@norria.com