Spring boot built Tomcat temp directory files can not be deleted lead to upload - Problem Analysis

1, problem

After the New Year, some operators said the staff reaction backstage operations can not upload pictures, and then view the log, find the error content /tmp/tomcat* 目录不存在.

surroundings:

  • spring boot 1.5.15
  • Centos7.6 (aliyun)

    2, issue resolved

  1. Why use this /tmp/tomcat*?
  2. That /tmp/tomcat*directory does not exist Why?

2.1 Why do we need to use this /tmp/tomcat*?

By default, spring boot of the built-in Tomcat, will /tmpcreate two directories /tmp/tomcat*and upload files, this directory is used to store compiled JSP.

2.2, the /tmp/tomcat*directory does not exist Why?

Because there is no mechanism by Linux are cleared.

This mechanism is what works:
First we have from the service systemd-tmpfiles-cleanto start.

[root@djx ~]# systemctl  status  systemd-tmpfiles-clean
● systemd-tmpfiles-clean.service - Cleanup of Temporary Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.service; static; vendor preset: disabled)
   Active: inactive (dead) since Tue 2020-02-25 09:10:36 CST; 12h ago
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)
  Process: 21819 ExecStart=/usr/bin/systemd-tmpfiles --clean (code=exited, status=0/SUCCESS)
 Main PID: 21819 (code=exited, status=0/SUCCESS)

Feb 25 09:10:36 djx systemd[1]: Starting Cleanup of Temporary Directories...
Feb 25 09:10:36 djx systemd[1]: Started Cleanup of Temporary Directories.

We can see this service today, 9:00 execution once, we continue to see what the service corresponding execution command?

cat  /usr/lib/systemd/system/systemd-tmpfiles-clean.service
#  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.

[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target time-sync.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle

Let's remember that this order is executed /usr/bin/systemd-tmpfiles --clean, we come under the care, the service associated with the timer/usr/lib/systemd/system/systemd-tmpfiles-clean.timer

cat  /usr/lib/systemd/system/systemd-tmpfiles-clean.timer 
#  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.

[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

We know this 启动后的15分钟或者距离上一次执行一天will execute on our command /usr/bin/systemd-tmpfiles --clean.

Then the above command specific and did what? We man systemd-tmpfilesfound some stuff in.

DESCRIPTION
       systemd-tmpfiles creates, deletes, and cleans up volatile and temporary files and directories, based on the configuration file format and location
       specified in tmpfiles.d(5).

       If invoked with no arguments, it applies all directives from all configuration files. If one or more filenames are passed on the command line, only
       the directives in these files are applied. If only the basename of a configuration file is specified, all configuration directories as specified in
       tmpfiles.d(5) are searched for a matching file.

From the above description I got two messages:

  1. Systemd-tmpfiles is used to create and clean up temporary directories and files.
  2. systemd-tmpfiles will obtain the configuration file from tmpfiles.d in.

Of course, we man systemd-tmpfilescan also understand some of the parameters command.

We then see tmpfiles.dthat we man tmpfiles.dcan see in tmpfiles.dthe role configuration is used to clean temporary files and directories. We can also see it in the configuration store

/etc/tmpfiles.d/*.conf
/run/tmpfiles.d/*.conf
/usr/lib/tmpfiles.d/*.conf

Here we can see the configuration file which related notes.

Next, we went to three directory listed above, look to see if a clean-up related /tmpthings.
Finally, /usr/lib/tmpfiles.d/tmp.confthere is found,

cat /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
v /var/tmp 1777 root root 30d

# 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

We can look at two of these configurations

v /tmp 1777 root root 10d  # 就是当 /tmp 目录不存在的时间进行创建(权限为777,用户和用户组为root),并清理/tmp下超过10天的文件。
x /tmp/systemd-private-%b-*  # 忽略清理的目录
X /tmp/systemd-private-%b-*/tmp # 这个只忽略目录,但不忽略该目录下面的内容

Meaning that clean up /tmpmore than 10 days did not change the file directory, but does not clean /tmp/systemd-private-%b-*and /tmp/systemd-private-%b-*/tmp.
Summarize the system will automatically clean up the / tmp directory for 10 days did not change files and directories

During the New Year our Tomcat generated directory does not change if the 10 days, it will also be deleted.

Third, the solution

Springboot modify the configuration, do not /tmpcreate directory

Configuration parameters:server.tomcat.basedir

Modify the mechanism clean up / tmp file below

In /usr/lib/tmpfiles.d/tmp.confthe following a increasex /tmp/tomcat*

Four, spring boot official answer

The official related issue:

  • https://github.com/spring-projects/spring-boot/issues/5009
  • https://github.com/spring-projects/spring-boot/issues/9616

In https://github.com/spring-projects/spring-boot/issues/9616,
at the bottom we can

You can see the list of releases that contain the fix in the commit that closed this issue. In the 2.1.x line it was fixed in 2.1.4.

See 2.1.4 version has solved this problem. In a version of the series, we can see in 1.5.20 also fixes, GitHub corresponding commit record

Reference: https: //www.cnblogs.com/samtech/p/9490166.html

Guess you like

Origin www.cnblogs.com/operationhome/p/12375003.html