Linux 下 /tmp目录清理规则

Linux 下 /tmp目录清理规则

/tmp目录下的hsperfdata_$user目录被删了,确认没人动tmp目录,查了一下是自动清理掉的,于是查找资料发现了tmp目录清理规则,记录一下

不同的 Linux 发行版其实对 /tmp 目录的清理方式有所不同:

在某些发行版里, tmp 目录原来只有在启动的时候才会被清理

 在 Debian-like 的系统,启动的时候才会清理 (规则定义在 /etc/default/rcS )

 在 RedHat-like 的系统,按文件存在时间定时清理 (RHEL6 规则定义在 /etc/cron.daily/tmpwatch ; RHEL7 以及 RedHat-like with systemd 规则定义在 /usr/lib/tmpfiles.d/tmp.conf , 通过 systemd-tmpfiles-clean.service 服务调用)

 在 CentOS 里,是按文件存在时间清理的 (通过 crontab 的配置 /etc/cron.daily 定时执行 tmpwatch 来实现)

 在 Gentoo 里也是启动清理,规则定义在 /etc/conf.d/bootmisc ,但 Gentoo 就是不走寻常路

对于那些只能开机清理临时文件的发行版,如果作为服务器,这种不重启就对临时文件目录的垃圾不问不管的做事风格实在是很不靠谱。不过从上面其他发行版大家估计也会发现,解决此问题的关键就在于 tmpwatch 和定时任务的配合使用。

tmpwatch 是专门用于解决“删除 xxx 天没有被访问/修改过的文件”这样需求的命令。使用方式也极其简单:

$ tmpwatch 30d /tmp/

注意在 Ubuntu 的 apt-get 是无法直接安装 tmpwatch 的,tmpwatch 在 Ubuntu 里叫 tmpreaper:

$ sudoapt-get installtmpreaper

$ sudotmpreaper 30d /tmp

CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系统服务有3个:

systemd-tmpfiles-setup.service :Create Volatile Files and Directories
systemd-tmpfiles-setup-dev.service:Create static device nodes in /dev
systemd-tmpfiles-clean.service :Cleanup of Temporary Directories
配置文件也有3个地方:

/etc/tmpfiles.d/*.conf
/run/tmpfiles.d/*.conf
/usr/lib/tmpfiles.d/*.conf
/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
v /tmp 1777 root root 7d
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
如你不想让系统自动清理/tmp下以tomcat开头的目录,那么增加下面这条内容到配置文件中即可:

x /tmp/tomcat.*


原文链接:https://blog.csdn.net/qq_21137441/article/details/90031390



原文链接:https://blog.csdn.net/Scott_kang/article/details/108419275