不同的 Linux 发行版其实对 /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 服务调用)
  • 在 CentOS6 里,也是按文件存在时间清理的 (通过 crontab 的配置 /etc/cron.daily 定时执行 tmpwatch 来实现)
  • 在centos7中,通过systemd-tmpfiles来进行清理
  • 在 Gentoo 里也是启动清理,规则定义在 /etc/conf.d/bootmisc

下面主要介绍centos7下清理方式

一、清理工具systemd-tmpfiles

红帽和fedora都为了方便管理/tmp目录,提供了系统级别的定时任务清理,简单的就是创建删除命令。

1
2
3
4
5
6
7
8
9
systemd-tmpfiles, systemd-tmpfiles-setup.service, systemd-tmpfiles-setup-dev.service, systemd-tmpfiles-clean.service, systemd-tmpfiles-clean.timer — 创建、删除、清理 易变文件与临时文件

systemd-tmpfiles [OPTIONS...] [CONFIGFILE...]

systemd-tmpfiles-setup.service 创建
systemd-tmpfiles-setup-dev.service 创建
systemd-tmpfiles-clean.service 清理
systemd-tmpfiles-clean.timer 定时清理

定时任务服务介绍

默认情况下只有开机启动的时候 (at boot time) 会执行清理,别的情况下怎么办?系统提供了定时任务

看下官网提供的守护进程 unit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[root@freddy ~]# systemctl status systemd-tmpfiles-clean.timer
● systemd-tmpfiles-clean.timer - Daily Cleanup of Temporary Directories
Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.timer; static; vendor preset: disabled)
Active: active (waiting) since Thu 2016-08-18 20:06:04 CEST; 2 weeks 2 days ago
Docs: man:tmpfiles.d(5)
man:systemd-tmpfiles(8)

Aug 18 20:06:04 freddy systemd[1]: Started Daily Cleanup of Temporary Directories.

[root@freddy ~]# cat /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
复制代码

这个定时任务是控制 clean 的任务,开机后 15 分钟会一直运行,之后是每 24 小时执行一次,该命令仅仅清理指定的目录 timer 是 systemctl 的一个定时任务,跟 cron 一样,不过是自己独有的方法。systemd-tmpfiles-clean.timer 表示定时执行systemd-tmpfiles-clean服务的/usr/bin/systemd-tmpfiles --clean命令。

二、配置文件参数和简单使用

相关的配置文件也有 3 个地方:

1
2
3
/etc/tmpfiles.d/*.conf
/run/tmpfiles.d/*.conf
/usr/lib/tmpfiles.d/*.conf

查看了对应的文件,发现在/usr/lib/tmpfiles.d/*.conf下有很多conf文件,都对应的不同应用 (目录) 的清理规则

/tmp 目录的清理规则主要取决于 / usr/lib/tmpfiles.d/tmp.conf 文件的设定,默认的配置内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#  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 10d # 清理/tmp下10天前的目录和文件
v /var/tmp 1777 root root 30d # 清理/var/tmp下30天前的目录和文件

# 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 开头的目录,那么增加下面这条内容到配置文件中即可:

1
x /tmp/tomcat.*

简单理解,定时清理时忽略该目录

摘自:
CentOS7的/tmp目录自动清理规则 - 掘金 (juejin.cn)