linux - Logrotate 重复文件

标签 linux centos centos7 logrotate

我有一个测试应用程序,它每小时运行一次,并在每次执行时使用唯一的日志文件。要清理日志,已设置以下 logrotate 配置:

{
        # Daily rotation with 1 week of backlog
        daily
        rotate 7
        maxage 7
        dateext
        compress
}

第一天日志文件被压缩(这没问题)但留下一个空文件,每隔一天文件被“清空”并压缩。这使得每个日志文件的 6 个文件填充了 FS 的 inode 表。这里有两个例子:

-rw-r--r-- 1 root root 1752 Feb 11 01:36 J20190211013601_Status.txt-20190212.gz
-rw------- 1 root root   20 Feb 12 03:33 J20190211013601_Status.txt-20190213.gz
-rw------- 1 root root   20 Feb 13 03:37 J20190211013601_Status.txt-20190214.gz
-rw------- 1 root root   20 Feb 14 03:10 J20190211013601_Status.txt-20190215.gz
-rw------- 1 root root   20 Feb 15 03:12 J20190211013601_Status.txt-20190216.gz
-rw------- 1 root root   20 Feb 16 03:36 J20190211013601_Status.txt-20190217.gz
-rw------- 1 root root   20 Feb 17 03:44 J20190211013601_Status.txt-20190218.gz
-rw------- 1 root root    0 Feb 18 03:24 J20190211013601_Status.txt

-rw-r--r-- 1 root root 1752 Feb 11 02:36 J20190211023601_Status.txt-20190212.gz
-rw------- 1 root root   20 Feb 12 03:33 J20190211023601_Status.txt-20190213.gz
-rw------- 1 root root   20 Feb 13 03:37 J20190211023601_Status.txt-20190214.gz
-rw------- 1 root root   20 Feb 14 03:10 J20190211023601_Status.txt-20190215.gz
-rw------- 1 root root   20 Feb 15 03:12 J20190211023601_Status.txt-20190216.gz
-rw------- 1 root root   20 Feb 16 03:36 J20190211023601_Status.txt-20190217.gz
-rw------- 1 root root   20 Feb 17 03:44 J20190211023601_Status.txt-20190218.gz
-rw------- 1 root root    0 Feb 18 03:24 J20190211023601_Status.txt

我怎样才能纠正这个问题,以便在压缩后删除文件

感谢您的时间和帮助,

最佳答案

这就是 logrotate 应该如何运作的;您的问题源于您每次运行应用程序时都使用唯一的文件名这一事实。

当 logrotate 第一次在每个日志上运行时,它将日志文件从“J20190211023601_Status.txt”移动到“J20190211023601_Status.txt-20190212.gz”,然后创建一个名为 J20190211023601_Status.txt 的新文件.

Logrotate 并没有固有的想法,即这些文件名是唯一的,因此永远不会被再次填充;它所看到的只是过去轮换的日志,因此必须根据您的配置再次轮换。

这里最简单的解决方案是为这个 logrotation 传递 nocreate 指令;这将防止创建新日志文件并随后轮换,同时仍然遵守先前轮换文件的 7 天期限:

{
  daily
  maxage 7
  dateext
  compress
  nocreate      
}

关于linux - Logrotate 重复文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54768250/

相关文章:

linux - 循环中的函数会破坏每隔一次迭代

ruby - RVM Ruby 默认不工作

linux - 如何找到 centOS 7 上安装了 binutils 的位置?

linux - centos7上的xrdp无法连接

gcc - nm devtoolset 的 .so 文件给出文件格式无法识别

c++ - 无法跟踪 Linux 中的内存泄漏

linux - VMWare guest 无法连接到主机服务器

postgresql - 如何让 pcp 自动将节点附加到 postgres pgpool?

python - 使用 apache 服务器时覆盖 python3 默认编码器

javascript - 如何去除 Centos 7 桌面 (Hot Corner) 右上角的效果?