如何清理/ tmp目录?

如何清理/tmp目录? 它是自动的吗? 如果是这样,它的清理频率如何?

注意! 至少从ubuntu 14.04开始,这个答案已经过时了。 查看当前情况的其他答案,如果certificate是正确的,那么就疯狂地投票。 也发表评论,所以我可以在这里链接到当前正确的答案。

对于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天,则将删除该文件。

$TMPTIME的默认值为0,这意味着/tmp每个文件和目录都将被删除。 $TMPTIME/etc/default/rcS定义的环境变量。

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

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

 /etc/default/rcS 

TMPTIME表示tmp dir可以在几天内清除

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

更改/tmp清理频率

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

TMPTIME=0的当前值表示在重新启动时删除文件,尽管文件的年龄。 将此值更改为其他(正数)将更改文件在/tmp可以存活的天数。

 TMPTIME=7 

此设置允许文件保留在/tmp直到它们为一周,然后在下次重新引导时删除它们。 负数( TMPTIME=-1 )告诉系统永远不会删除/tmp任何内容。 这可能不是你想要的,但可用。

我在Ubuntu 16.10上查看这个。 我可以certificate编辑/ 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 

如果您不想删除文件,请将“20d”替换为“ – ”。 这是我的最大努力,手册页几乎无法穿透细节。

新设置的优点是,即使系统未重新启动,文件清理程序仍可以运行(如始终在服务器上)。 我认为这是一个很大的优点。

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

在14.04之前:

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

在我们运行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 {} \; 

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

systemd Ubuntu(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 

 $ 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 

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

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