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
几天,它将被删除。
的默认值为$TMPTIME
0,表示/tmp
删除其中的每个文件和目录。$TMPTIME
中定义的环境变量/etc/default/rcS
。
小智 150
默认情况下,每次启动时都会清除该目录,因为TMPTIME
默认情况下为 0。
您可以在此处更改以下文件中的时间:
/etc/default/rcS
Run Code Online (Sandbox Code Playgroud)
TMPTIME
说在几天内清除 tmp 目录的频率
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
. 这可能不是您想要的,但可用。
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”替换为“-”。这是我最大的努力,那个手册页的细节几乎难以理解。
新设置的优点是即使系统没有重新启动,文件清理器仍然可以运行(就像在服务器始终运行的情况下一样)。我认为这是一个很大的优势。
mur*_*uru 13
在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
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
)。
小智 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 服务器,我们不想出于显而易见的原因重新启动它。
归档时间: |
|
查看次数: |
439192 次 |
最近记录: |