CentOS6 以下系统(含)使用 watchtmp + cron 来实现定时清理临时档案的效果,这点在 CentOS7 发生了变化,在 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 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 开头的目录,那么增加下面这条内容到配置档案中即可:
x /tmp/tomcat.*