1、CentOS 7
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
也就是CentOS 7 系统有一个默认定时器会每隔 1 天执行一次临时目录清理操作,把 /tmp 目录下所有超过 10 天没有任何变动的子目录全部删除。
可以配置这个文件,比如你不想让系统自动清理/tmp下以tomcat开头的目录,那么增加下面这条内容到配置文件中即可:
- x /tmp/tomcat.*
2、CentOS 6
CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,该目录下的脚本每天执行一次。
如果要修改的话,可以修改此 tmpwatch 脚本(用 root 用户修改或其他用户 sudo 修改):
找到类似该语句:
- /usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
- -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
- -X '/tmp/hsperfdata_*' -X '/tmp/.hdb*lock' -X '/tmp/.sapstartsrv*.log' \
- 10d /tmp
为该语句添加 -X '/tmp/tomcat*' 参数,以便在清除时忽略所有以 /tmp/tomcat 开头的目录和文件:
- /usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \
- -x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \
- -X '/tmp/hsperfdata_*' -X '/tmp/.hdb*lock' -X '/tmp/.sapstartsrv*.log' \
- -X '/tmp/tomcat*' \
- 10d /tmp