详解Centos 7和Centos 6系统的/tmp目录自动清理规则及区别

运维 系统运维 Linux
分享最近应用碰到的一个奇怪bug,一开始以为是代码上的问题,找了一段时间发现居然是因为系统的一个自动清理规则导致,下面一起来看看吧~

概述

分享最近应用碰到的一个奇怪bug,一开始以为是代码上的问题,找了一段时间发现居然是因为系统的一个自动清理规则导致,下面一起来看看吧~

一、应用报错: 

logwire.core.exceptions.GeneralUnhandledException: 服务端未处理异常  
...  
Caused by: org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is  
java.io.IOException: The temporary upload location [/tmp/tomcat.5454715517323162300.8031/work/Tomcat/localhost/ROOT] is not valid  
....  
Caused by: java.io.IOException: The temporary upload location [/tmp/tomcat.5454715517323162300.8031/work/Tomcat/localhost/ROOT] is not valid 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

报错截图:

二、思路

CentOS 7 系统有一个默认定时器会每隔 1 天执行一次临时目录清理操作,把 /tmp 目录下所有超过 10 天没有任何变动的子目录全部删除。

若某个 Logwire 系统的用户操作不太活跃,则 tomcat 运行临时目录(形如: /tmp/tomcat.xxx)会被删除。这时候如果有用户试图上传文件,则服务端会报错:

The temporary upload location [/tmp/tomcat.5454715517323162300.8031/work/Tomcat/localhost/ROOT] is not valid

三、处理过程

在 /usr/lib/tmpfiles.d/tmp.conf 文件中增加配置项: 

x /tmp/tomcat* 
  • 1.

以上配置表示在清理临时目录时忽略所有以 /tmp/tomcat 开头的目录和文件

四、CentOS系统的/tmp目录自动清理规则

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 
  • 1.
  • 2.
  • 3.

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

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

/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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

也就是CentOS 7 系统有一个默认定时器会每隔 1 天执行一次临时目录清理操作,把 /tmp 目录下所有超过 10 天没有任何变动的子目录全部删除。

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

x /tmp/tomcat.* 
  • 1.

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 
  • 1.
  • 2.
  • 3.
  • 4.

为该语句添加 -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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

觉得有用的朋友多帮忙转发哦!后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~ 

 

责任编辑:庞桂玉 来源: 今日头条
相关推荐

2016-08-12 14:37:52

Linux tmpRedhattmpwatch

2014-08-07 10:07:13

RHELCentOS

2012-08-08 10:10:30

CentOS 6操作系统

2021-08-11 06:16:27

CentOS 7 MongodbC++

2023-06-01 08:15:04

CentOS红帽

2010-01-14 14:32:07

CentOS ligh

2010-03-31 15:24:15

CentOS系统

2010-03-31 15:08:28

CentOS系统

2022-07-26 15:22:46

CentOS 7脚本Linux

2013-05-03 10:21:53

CentOSRedHat Linu

2020-06-09 07:00:00

RHELCentOSFedora

2024-03-01 20:28:54

CentOS 7文件操作search

2010-03-23 08:46:07

Windows 7系统优化

2014-08-06 16:32:50

2017-02-27 11:06:59

RHEL7CentOS7密码

2013-11-11 09:34:51

超6类7类网线

2024-04-10 13:50:41

CentOSUbuntu操作系统

2012-07-11 17:09:34

Windows 7CentOS

2023-05-20 17:48:39

Linux/tmp/var/tmp

2010-08-03 13:54:00

点赞
收藏

51CTO技术栈公众号