自动清理tmp文件

背景

supervisor默认配置中,其启动的sock等都会放到tmp目录,而tmp目录会自动清理导致无法使用supervisorctl命令,此时:

  1. 修改supervisor.conf文件,修改到/var/run//var/log/目录,具体配置就不进行贴了,简单直接搜索tmp进行修改即可。
  2. 重启supervisor服务,记得kill原来服务。

但是,为什么系统会自动清理tmp目录呢?

RHEL\CentOS\Fedora系统

CentOS 6中,使用tmpwatch命令进行删除。

tmpwatch - removes files which haven’t been accessed for a period of time

tmpwatch [-u|-m|-c] [-MUadfqstvx] [–verbose] [–force] [–all] [–nodirs] [–nosymlinks] [–test] [–fuser] [–quiet] [–atime|–mtime|–ctime] [–dirmtime] [–exclude path] [–exclude-user user] time dirs

/etc/cron.daily/目录下会生成tmpwatch文件:

1
2
3
4
5
6
7
8
9
10
11
#! /bin/sh
flags=-umc
/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_*' 10d /tmp
/usr/sbin/tmpwatch "$flags" 30d /var/tmp
for d in /var/{cache/man,catman}/{cat?,X11R6/cat?,local/cat?}; do
if [ -d "$d" ]; then
/usr/sbin/tmpwatch "$flags" -f 30d "$d"
fi
done

将自动删除240小时未访问文件,tmpwatch工具可以从指定目录递归搜索并删除一段时间未访问文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
-u, --atime 基于访问时间来删除文件,默认的。
-m, --mtime 基于修改时间来删除文件。
-c, --ctime 基于创建时间来删除文件,对于目录,基于mtime。
-M, --dirmtime 删除目录基于目录的修改时间而不是访问时间。
-a, --all 删除所有的文件类型,不只是普通文件,符号链接和目录。
-d, --nodirs 不尝试删除目录,即使是空目录。
-d, --nosymlinks 不尝试删除符号链接。
-f, --force 强制删除。
-q, --quiet 只报告错误信息。
-s, --fuser 如果文件已经是打开状态在删除前,尝试使用“定影”命令。默认不启用。
-t, --test 仅作测试,并不真的删除文件或目录。
-U, --exclude-user=user 不删除属于谁的文件。
-v, --verbose 打印详细信息。
-x, --exclude=path 排除路径,如果路径是一个目录,它包含的所有文件被排除了。如果路径不存在,它必须是一个绝对路径不包含符号链接。
-X, --exclude-pattern=pattern 排除某规则下的路径。

CentOS 7

CentOS 7中会注意到一点,在系统中默认会清理tmp目录,而且没有安装tmpwatch,那么CentOS 7中是通过什么来实现的呢?

1
2
3
4
5
6
7
8
9
10
11
# systemctl status systemd-tmpfiles-clean.service
systemd-tmpfiles-clean.service - Cleanup of Temporary Directories
Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-clean.service; static)
Active: inactive (dead) since Thu 2015-12-10 11:54:02 HKT; 5h 12min ago
Docs: man:tmpfiles.d(5)
man:systemd-tmpfiles(8)
Process: 6523 ExecStart=/usr/bin/systemd-tmpfiles --clean (code=exited, status=0/SUCCESS)
Main PID: 6523 (code=exited, status=0/SUCCESS)

Dec 10 11:54:02 hwangjr systemd[1]: Starting Cleanup of Temporary Directories...
Dec 10 11:54:02 hwangjr systemd[1]: Started Cleanup of Temporary Directories.

没错,就是通过这个服务来实现的,此服务为/usr/lib/systemd/system/systemd-tmpfiles-clean.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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
Wants=local-fs.target
After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target
Before=sysinit.target shutdown.target
ConditionDirectoryNotEmpty=|/usr/lib/tmpfiles.d
ConditionDirectoryNotEmpty=|/usr/local/lib/tmpfiles.d
ConditionDirectoryNotEmpty=|/etc/tmpfiles.d
ConditionDirectoryNotEmpty=|/run/tmpfiles.d

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

# 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

可以看到在启动15分钟之后会清理,每隔1天清理一次。更多详细信息systemd-tmpfiles

Debian\Ubuntu系统

Ubuntu系统中每次开机会进行清除,具体配置为rcS文件的TMPTIME值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
$ cat /etc/default/rcS
#
# /etc/default/rcS
#
# Default settings for the scripts in /etc/rcS.d/
#
# For information about these variables see the rcS(5) manual page.
#
# This file belongs to the "initscripts" package.

# delete files in /tmp during boot older than x days.
# '0' means always, -1 or 'infinite' disables the feature
#TMPTIME=0

# spawn sulogin during boot, continue normal boot if not used in 30 seconds
#SULOGIN=no

# do not allow users to log in until the boot has completed
#DELAYLOGIN=no

# assume that the BIOS clock is set to UTC time (recommended)
UTC=yes

# be more verbose during the boot process
#VERBOSE=no

# automatically repair filesystems with inconsistencies during boot
#FSCKFIX=no

也就是在x天启动之后会自动清理tmp目录,0代表一直清理,-1代表取消此特性。

注意点

supervisor

修改默认配置的/tmp/目录为/var/run//var/log目录。

mysql

默认将pidsocket创建在tmp目录,将两个此文件排除在外,否则mysql重启或使用socket登陆时提示找不到文件,可通过-U mysql

REF

Index of /download/SRPMS/tmpwatch
rpms/tmpwatch - git.centos.org
tmp目录自动清除和tmpwatch命令 - 运维生存时间
关于Linux系统清理/tmp/文件夹,你可能想知道的 | 羽飞博客