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.*