美文网首页
/tmp,消失的文件

/tmp,消失的文件

作者: halfempty | 来源:发表于2021-10-21 16:08 被阅读0次

    1. 问题

    Linux的/tmp目录想必都用过

    The /tmp directory in Linux based systems contains necessary files that are temporarily required by the system as well as other software and applications running on the machine.

    使用tomcat的人可能遇到, IOException: The temporary upload location xxx is not valid
    使用hadoop的人可能遇到, /tmp/hadoop-xxx.pid, No such file
    那么文件去哪儿呢?

    2. 分析

    为了防止系统磁盘被/tmp耗尽, 需要定时清理, 于是出现了误删操作

    3. 原理

    centos7通过systemd-tmpfiles实现, 清理的动作来自systemd-tmpfiles-clean

    [root@localhost ~]# systemctl list-unit-files |grep tmpfile
    systemd-tmpfiles-clean.service                static  
    systemd-tmpfiles-setup-dev.service            static  
    systemd-tmpfiles-setup.service                static  
    systemd-tmpfiles-clean.timer                  static  
    

    3.1. 本质

    本质是执行/usr/bin/systemd-tmpfiles --clean

    man systemd-tmpfiles

    --clean
    If this option is passed, all files and directories with an age parameter configured will be cleaned up.

    [root@localhost ~]# systemctl cat systemd-tmpfiles-clean
    [Unit]
    Description=Cleanup of Temporary Directories
    Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
    DefaultDependencies=no
    Conflicts=shutdown.target
    After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target time-sync.target
    Before=shutdown.target
    
    [Service]
    Type=oneshot
    ExecStart=/usr/bin/systemd-tmpfiles --clean
    IOSchedulingClass=idle
    

    3.2. 清理的对象

    既然是清理, 那么哪些应该清理, 哪些应该保留呢?

    配置文件路径/usr/lib/tmpfiles.d, /etc/tmpfiles.d, 其中/etc/tmpfiles.d优先级更高, 用户通过修改此处文件实现覆盖

    [root@localhost ~]# ll /usr/lib/tmpfiles.d/
    total 68
    -rw-r--r--. 1 root root  464 Nov  6  2016 etc.conf
    -rw-r--r--. 1 root root   39 Nov  5  2016 initscripts.conf
    -rw-r--r--. 1 root root 1181 Nov  6  2016 legacy.conf
    -rw-r--r--. 1 root root   34 Mar 31  2020 libselinux.conf
    -r--r--r--. 1 root root   61 Nov 11  2016 lvm2.conf
    -rw-r--r--. 1 root root  110 Nov  5  2016 pam.conf
    -rw-r--r--. 1 root root   61 Jan 26  2014 ppp.conf
    -rw-r--r--. 1 root root   16 Nov  5  2016 python.conf
    -rw-r--r--. 1 root root   22 Nov  5  2016 rpm.conf
    -rw-r--r--. 1 root root  228 Nov  6  2016 sap.conf
    -rw-r--r--. 1 root root  137 Nov 16  2020 selinux-policy.conf
    -rw-r--r--. 1 root root 1393 Nov  6  2016 systemd.conf
    -rw-r--r--. 1 root root  496 Nov  6  2016 systemd-nologin.conf
    -rw-r--r--. 1 root root  638 Nov  6  2016 tmp.conf
    -rw-r--r--. 1 root root   56 Jun 16  2016 tuned.conf
    -rw-r--r--. 1 root root  563 Nov  6  2016 var.conf
    -rw-r--r--. 1 root root  623 Nov  6  2016 x11.conf
    

    3.3. 清理策略

    知道清理的对象后, 下一步应当如何清理呢, 清理的规则是什么?

    我们看下/tmp的规则
    /tmp保留10天, /var/tmp目录保留30天 (起始时间取文件/目录的创建/访问/修改的最新时间)
    规则的具体含义, 请查阅man tmpfiles.d

    [root@localhost ~]# cat /usr/lib/tmpfiles.d/tmp.conf 
    v /tmp 1777 root root 10d
    v /var/tmp 1777 root root 30d
    
    # Exclude namespace mountpoints created with PrivateTmp=yes
    x /tmp/systemd-private-%b-*
    X /tmp/systemd-private-%b-*/tmp
    x /var/tmp/systemd-private-%b-*
    X /var/tmp/systemd-private-%b-*/tmp
    

    3.4. 何时执行

    执行周期由systemd-tmpfiles-clean.timer决定
    默认是从服务器启动后15分钟作起点, 每隔1天执行一次

    [root@localhost ~]# systemctl cat systemd-tmpfiles-clean.timer
    # /usr/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
    

    相关文章

      网友评论

          本文标题:/tmp,消失的文件

          本文链接:https://www.haomeiwen.com/subject/mnntaltx.html