AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 20783
Accepted
Olivier Lalonde
Olivier Lalonde
Asked: 2011-01-10 11:26:19 +0800 CST2011-01-10 11:26:19 +0800 CST 2011-01-10 11:26:19 +0800 CST

/tmp 目录是如何清理的?

  • 772

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

filesystem
  • 8 8 个回答
  • 480273 Views

8 个回答

  • Voted
  1. Best Answer
    Lesmana
    2011-01-10T16:17:59+08:002011-01-10T16:17:59+08:00

    笔记!这个答案至少从 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每次挂载时,脚本都会由暴发户运行。实际上,这意味着在每次启动时。

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

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

    • 212
  2. therufa
    2011-01-10T11:35:02+08:002011-01-10T11:35:02+08:00

    该目录在每次启动时默认清除,因为TMPTIME默认为 0。

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

    /etc/default/rcS
    

    TMPTIME表示几天内清除 tmp 目录的频率

    • 152
  3. hhlp
    2011-01-10T11:35:16+08:002011-01-10T11:35:16+08:00

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

    更改/tmp清理频率

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

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

    TMPTIME=7
    

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

    • 75
  4. pauljohn32
    2016-12-05T20:10:40+08:002016-12-05T20:10:40+08:00

    我正在 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
    

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

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

    • 69
  5. Martin Schröder
    2016-04-20T04:00:24+08:002016-04-20T04:00:24+08:00

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

    • 27
  6. muru
    2017-12-07T09:11:15+08:002017-12-07T09:11:15+08:00

    在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
    

    和

    $ 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您可以使用和使用各种 systemdTimer配置选项来更改计时器行为本身(请参阅 参考资料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 的其他答案。

    • 18
  7. Utku Demir
    2011-01-10T11:34:38+08:002011-01-10T11:34:38+08:00

    14.04 之前:

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

    • 12
  8. Paul
    2011-09-28T20:54:41+08:002011-09-28T20:54:41+08:00

    在我们的一台运行 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 服务器,我们不想重新启动它,原因很明显。

    • 6

相关问题

  • 当另一个文件系统安装在路径上时是否可以删除文件?

  • “/var/lib/dpkg/updates”文件夹中存储了什么?

  • 如何使文件夹与外部 USB 硬盘保持同步?

  • 为什么 Ubuntu 不再将文件大小单位测量为字节、兆字节、千兆字节等?

  • 如何避免启动时出现“S to Skip”消息?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    我需要什么命令来解压缩/提取 .tar.gz 文件?

    • 8 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Martin Hope
    EmmyS 我需要什么命令来解压缩/提取 .tar.gz 文件? 2011-02-09 14:50:41 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve