/tmp 目录是如何清理的?

Oli*_*nde 352 filesystem tmp

/tmp目录是如何清理的?是自动的吗?如果有,多久清理一次?

les*_*ana 211

笔记!这个答案至少从 ubuntu 14.04 开始就已经过时了。查看当前情况的其他答案,如果它们被证明是正确的,则疯狂地投票。同时发表评论,以便我可以在此处放置当前正确答案的链接。

对于 14.04,请参阅https://askubuntu.com/a/759048/1366

对于 16.10,请参阅https://askubuntu.com/a/857154/453746


2011年的旧答案:

清理/tmp是由 upstart 脚本完成的/etc/init/mounted-tmp.conf。每次/tmp安装时,该脚本都由 upstart 运行。实际上,这意味着在每次启动时。

该脚本大致执行以下操作:如果文件/tmp超过$TMPTIME几天,它将被删除。

的默认值为$TMPTIME0,表示/tmp删除其中的每个文件和目录。$TMPTIME中定义的环境变量/etc/default/rcS

  • 在 ubuntu 16.04 中,tmpreaper 因不安全而被放弃,请参阅:https://fossies.org/linux/tmpreaper/debian/README.security (4认同)
  • 不。在 Ubuntu 16.04 中,它只是被 systemd 机制取代。阅读该文档,您实际上会发现解释为什么声称不安全的安全分析存在缺陷。 (2认同)

小智 150

默认情况下,每次启动时都会清除该目录,因为TMPTIME默认情况下为 0。

您可以在此处更改以下文件中的时间:

/etc/default/rcS
Run Code Online (Sandbox Code Playgroud)

TMPTIME 说在几天内清除 tmp 目录的频率

  • 每次启动时清除对于从未重新启动的机器(如服务器)来说并不理想。我的 /tmp 中有超过 500,000 个文件占用 5Gb 空间,因为我的服务器有 378 天的正常运行时间。我不愿意重新启动它,仅仅是因为在重新启动时清除这些文件需要几个小时。 (13认同)
  • 在你的情况下,你应该给 `tmpreaper` 一个机会。 (11认同)
  • `cat: /etc/default/rcS: 没有这样的文件或目录` (3认同)

hhl*_*hlp 72

虽然该/tmp文件夹不是长期存储文件的地方,但有时您希望将文件保存的时间比下次重启时长一点,这是 Ubuntu 系统的默认设置。我知道一两次我/tmp在测试期间下载了一些东西,在进行更改后重新启动,然后再次丢失了原始数据。如果您想将/tmp文件保留更长时间,可以更改此设置。

更改/tmp清理频率

告诉您的系统/tmp在重新启动时清除的默认设置保存在该/etc/default/rcS文件中。我们要查看的值是TMPTIME

TMPTIME=0尽管文件已过期,但当前值表示在重新启动时删除文件。将此值更改为其他(正)数将更改文件在/tmp.

TMPTIME=7
Run Code Online (Sandbox Code Playgroud)

此设置将允许文件保留/tmp一周,然后在下次重新启动时删除它们。负数 ( TMPTIME=-1) 告诉系统永远不要删除/tmp. 这可能不是您想要的,但可用。

  • 如果你不想自动删除一个文件,把它放在 `/var/tmp` 而不是 `/tmp`。 (8认同)

pau*_*n32 66

我正在 Ubuntu 16.10 上检查这个。我可以证明编辑 /etc/default/rcS 不再有任何影响,并且无论您在该文件中放什么,tmp 中的文件都会被重新启动清除。正如其他人所说,不再使用 tmpreaper。

我认为正确的答案是 Ubuntu 16.10 有一个新设置。有一个文件夹 /etc/tmpfiles.d,记录在手册页“tmpfiles.d”中。在该文件夹中,应该放置一个配置文件来控制是否要删除 /tmp。这就是我正在做的事情,以阻止重新启动以擦除 /tmp 中的文件,除非它们已经 20 天了:

#/etc/tmpfiles.d/tmp.conf

d /tmp 1777 root root 20d
Run Code Online (Sandbox Code Playgroud)

如果您不想删除文件,请将“20d”替换为“-”。这是我最大的努力,那个手册页的细节几乎难以理解。

新设置的优点是即使系统没有重新启动,文件清理器仍然可以运行(就像在服务器始终运行的情况下一样)。我认为这是一个很大的优势。

  • `人tmpfiles.d` (10认同)
  • 另外值得注意的是:您可以通过手动运行清理作业来测试您的配置:`systemctl start systemd-tmpfiles-clean` (3认同)
  • @kapad:我刚刚确认这也适用于 Ubuntu 18.04。 (3认同)
  • 创建`/etc/tmpfiles.d/tmp.conf` 覆盖`/usr/lib/tmpfiles.d/tmp.conf` 导致其他目录的配置如果不复制就会丢失(例如,`/var/tmp /systemd-private-%b-*`)。见[源](https://git.launchpad.net/ubuntu/+source/systemd/tree/tmpfiles.d./tmp.conf?h=ubuntu/bionic-updates)。 (2认同)

Mar*_*der 26

在 Ubuntu 14.04 中,这是由 完成的tmpreaper,它每天由 cron(来自/etc/cron.daily)调用。该程序可以通过/etc/default/rcS和进行配置/etc/tmpreaper.conf

  • 注意:你不应该在 16 和 18 上安装 tmpreaper,b/c 它有一个内置系统 (2认同)

mur*_*uru 13

systemdUbuntu(15.10 和更新版本)中,这是由 systemd 使用systemd-tmpfiles-clean服务和计时器完成的:

$ systemctl cat systemd-tmpfiles-clean.service 
# /lib/systemd/system/systemd-tmpfiles-clean.service
#  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=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs.target time-sync.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle
Run Code Online (Sandbox Code Playgroud)

$ systemctl cat systemd-tmpfiles-clean.timer  
# /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
Run Code Online (Sandbox Code Playgroud)

所以systemd-tmpfiles-clean在关机时运行,否则每天运行一次。它清理的文件可以使用另一个答案中/etc/tmpfiles.d提到的进行扩展。

您可以使用systemctl edit systemd-tmpfiles-clean.timer和 使用各种 systemdTimer配置选项来更改计时器行为本身(请参阅参考资料man 5 systemd.timer)。


小智 11

14.04 之前:

每次重新启动时都会清理它。


小智 6

在我们的一台运行 Ubuntu 的服务器上,我们有一个脚本来删除 /tmp 中的文件,它每晚运行。

脚本是:

#!/bin/sh
# Clean file and dirs more than 3 days old in /tmp nightly

/usr/bin/find /tmp -type f -atime +2 -mtime +2  |xargs  /bin/rm -f &&

/usr/bin/find /tmp -type d -mtime +2 -exec /bin/rm -rf '{}' \; &&

/usr/bin/find /tmp -type l -ctime +2 |xargs /bin/rm -f &&

/usr/bin/find -L /tmp -mtime +2 -print -exec rm -f {} \;
Run Code Online (Sandbox Code Playgroud)

只需将上面的内容保存到文件 chmod 775 该文件并创建一个 cron 条目来运行它。由于这是一个 Web 服务器,我们不想出于显而易见的原因重新启动它。

  • 最后一行非常危险。通常,每个人都可以运行 `ln -s /usr /tmp/kaboom` 甚至 `ln -s /* /tmp/` ... (12认同)
  • 您最好使用 [tmpwatch](http://linux.die.net/man/8/tmpwatch)。 (7认同)