Linux中如何修改/tmp目录清理规则

519 阅读1分钟

1.修改配置文件

centos7中/tmp目录通过systemd-tmpfiles-clean.service服务进行清理,清理规则配置文件为/usr/lib/tmpfiles.d/tmp.conf,调用命令为/usr/bin/systemd-tmpfiles --clean,执行时间依靠systemd-tmpfiles-clean.timer进行管理。centos6则是根据文件的访问时间等条件使用tmpwatch命令进行/tmp目录的清理,可以使用crond daemon进行定期执行。

#  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-*      #保留匹配/tmp/systemd-private-%b-*的目录和文件
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*  #保留匹配/var/tmp/systemd-private-%b-*的目录和文件
X /var/tmp/systemd-private-%b-*/tmp

2.延伸

曾有研发同学向我反映一个问题,tomcat会在/tmp目录下生成一个上传目录,但目录会被定时清理,有没有方法可以解决。报错如下: The temporary upload location [/tmp/tomcat.4179673622814334263.26013/work/Tomcat/localhost/ROOT] is not valid 这里就可以用到👆介绍的方法,修改/tmp目录规则: vim /usr/lib/tmpfiles.d/tmp.conf在文件最后一行添加: x /tmp/tomcat.*即可。

本文主要从运维方向解决该问题,也可以从代码上进行修改tomcat的basedir解决此问题