ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

Linux /tmp,消失的文件

2022-01-10 09:03:55  阅读:201  来源: 互联网

标签:tmp systemd -- 消失 tmpfiles conf Linux root


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,systemd,--,消失,tmpfiles,conf,Linux,root
来源: https://blog.csdn.net/weixin_44129801/article/details/122403271

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有