/tmp目录是如何清理的?是自动的吗?如果是的话,清理频率是多少?
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
表示临时文件夹应该以天为单位清理的频率
tmpreaper
一个机会。 - qbitmpwatch
应该是一个合适的工具。 - ArekBulskicat: /etc/default/rcS: No such file or directory
- user677955TMPTIME=7
/etc/init/mounted-temp.conf
,但它有一行start on mounted MOUNTPOINT=/tmp
让我觉得它不适用。 - enzotib/tmp
目录中放置文件时,可以将其放在/var/tmp
目录中。 - Gilles 'SO- stop being evil'/var/tmp
目录下(正如Gilles所说)。然后你可以像Arch Linux一样将/tmp
挂载为tmpfs。 - Guildenstern#/etc/tmpfiles.d/tmp.conf
d /tmp 1777 root root 20d
man tmpfiles.d
- Martin Schröderd /tmp/ - - - 20d
。 - Dave Yarwoodsystemctl start systemd-tmpfiles-clean
。 - Dave Yarwood/etc/tmpfiles.d/tmp.conf
会覆盖 /usr/lib/tmpfiles.d/tmp.conf
,导致其他目录的配置丢失,除非进行复制(例如 /var/tmp/systemd-private-%b-*
)。请参考源代码。 - Mihai Capotăq /tmp 1777 root root 10d
和q /var/tmp 1777 root root 30d
。 - mgutttmpreaper
来完成的,它由cron每天调用(从/etc/cron.daily
)。该程序可以通过/etc/default/rcS
和/etc/tmpreaper.conf
进行配置。$ 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
systemctl edit systemd-tmpfiles-clean.timer
更改计时器行为,还可以使用各种systemd Timer
配置选项(请参见man 5 systemd.timer
)。systemd-tmpfiles-clean
在关机时运行,并且在其他时间每天运行一次。它清理的文件可以使用/etc/tmpfiles.d
进行扩展(在另一个答案中提到)。/etc/tmpfiles.d
为空。定义/tmp
策略的文件在这里:/usr/lib/tmpfiles.d/tmp.conf
# 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.
# See tmpfiles.d(5) for details
# Clear tmp directories separately, to make them easier to override
D /tmp 1777 root root -
#q /var/tmp 1777 root root 30d
所以,正如已经提到的那样,要覆盖它,您可以参考关于tmpfiles.d的其他答案。
/tmp
仍然没有清理掉。我需要它被清理掉。即使手动启动sudo systemctl start systemd-tmpfiles-clean
也没有起作用。有什么想法为什么会这样? - user2932688每次重启后都会被清理。
tmpreaper
。 - Ken Sharp#!/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服务器,出于明显的原因,我们不希望重新启动它。
ln -s /usr /tmp/kaboom
甚至ln -s /* /tmp/
... - Daniel Alder
tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0
。 - Anonymous