如何编辑 systemd-tmpfiles-clean 的计时器?

arl*_*dia 7 tmp systemd-timer

我正在尝试将 Apache PrivateTmp 文件的清理间隔从默认的 30 天更改为 6 小时。我读到它来编辑时间间隔,我应该在/etc/tmpfiles.d/tmp.conf而不是编辑中设置一个覆盖文件/usr/lib/tmpfiles.d/tmp.conf,所以我用以下几行创建了该文件:

# override the default cleanup intervals
v /tmp 1777 root root 6h
v /var/tmp 1777 root root 6h
Run Code Online (Sandbox Code Playgroud)

现在,如果我运行systemd-tmpfiles --clean,预期的文件将被删除,所以这部分工作正常。

但是,/usr/lib/systemd/system/systemd-tmpfiles-clean.timer已经OnUnitActiveSec设置为1d。我认为这意味着我 6 小时的清理间隔将有效地限制为每天一次。

我可以将该计时器间隔更改为 6 小时或更短,但是我应该直接编辑此文件,还是创建类似于/etc/tmpfiles.d?

更新:这个问题被标记为重复,但我在链接的问题中没有看到任何关于我是否应该像文件一样使用覆盖文件的tmp.conf内容。

解决方案:显然我无法将此作为答案发布,因为该问题已被标记为重复。但这就是我创建覆盖文件来更改计时器间隔的方式:

将现有的计时器文件复制到相应的覆盖目录:

sudo cp /usr/lib/systemd/system/systemd-tmpfiles-clean.timer /etc/systemd/system

编辑新副本(将 1d 值更改为 1h):

sudo nano /etc/systemd/system/systemd-tmpfiles-clean.timer

加载新的计时器文件:

sudo systemctl daemon-reload

确认已加载新的计时器间隔:

sudo systemctl list-timers

小智 5

这个答案有点晚了,但我会把它留在这里以防其他人偶然发现。

我认为了解systemd覆盖如何工作的底层机制很重要。您的解决方案说明您已经弄清楚了低级实现细节以及如何手动创建覆盖,这是一件好事。

为了完整性和传播最佳实践,人们应该使用内置systemctl函数来创建覆盖(如评论中提到的@muru)。例如:

sudo systemctl edit systemd-tmpfiles-clean.timer
Run Code Online (Sandbox Code Playgroud)

例如,这可确保在文件上正确设置权限,并通过依赖底层抽象减少引入错误的可能性。

如果要查看构成本单元的组件,请systemctl cat按如下方式使用:

sudo systemctl cat systemd-tmpfiles-clean.timer 

# /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

# /etc/systemd/system/systemd-tmpfiles-clean.timer.d/override.conf
[Timer]
OnBootSec=15min
OnUnitActiveSec=60min
Run Code Online (Sandbox Code Playgroud)