CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系统服务有3个:

    1. systemd-tmpfiles-setup.service Create Volatile Files and Directories
    2. systemd-tmpfiles-setup-dev.serviceCreate static device nodes in /dev
    3. systemd-tmpfiles-clean.service Cleanup of Temporary Directories

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

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

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

    1. # This file is part of systemd.
    2. #
    3. # systemd is free software; you can redistribute it and/or modify it
    4. # under the terms of the GNU Lesser General Public License as published by
    5. # the Free Software Foundation; either version 2.1 of the License, or
    6. # (at your option) any later version.
    7. # See tmpfiles.d(5) for details
    8. # Clear tmp directories separately, to make them easier to override
    9. v /tmp 1777 root root 10d # 清理/tmp下10天前的目录和文件
    10. v /var/tmp 1777 root root 30d # 清理/var/tmp下30天前的目录和文件
    11. # Exclude namespace mountpoints created with PrivateTmp=yes
    12. x /tmp/systemd-private-%b-*
    13. X /tmp/systemd-private-%b-*/tmp
    14. x /var/tmp/systemd-private-%b-*
    15. X /var/tmp/systemd-private-%b-*/tmp

    我们可以配置这个文件,比如你不想让系统自动清理/tmp下以tomcat开头的目录,那么增加下面这条内容到配置文件中即可:

    1. x /tmp/tomcat.*