linux tmp removed - yaokun123/php-wiki GitHub Wiki

Linux自动清除tmp目录下文件

一、常见Linux 发行版 /tmp 清理策略

发行版 何时删除 如何配置
Ubuntu 14.04 系统开机引导 在 /etc/default/rcS 中指定TMPTIME 参数
Ubuntu 16.10 系统开机引导 在 /etc/tmpfiles.d/tmp.conf 中指定
RedHat-like RHEL6 每日定时 /etc/cron.daily/tmpwatch
RedHat-like RHEL7 每日定时 /usr/lib/tmpfiles.d/tmp.conf
Gentoo / /etc/conf.d/bootmisc

二、RHEL7清除原理

systemd-tmpfiles-clean.service服务

服务: systemd-tmpfiles-clean.service

服务何时被执行呢?

Linux下该服务的执行可以根据systemd-tmpfiles-clean.timer进行管理

[root@shuidi-shell-001:/usr/lib/tmpfiles.d]# 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


# OnBootSec 表示相对于机器被启动的时间点
# 表示相对于匹配单元(本标签下Unit=指定的单元)最后一次被启动的时间点

上述配置文件表示两种情况会执行该服务

1、开机15分钟执行服务 2、离上次执行该服务1天后执行服务

服务如何执行呢?

[root@shuidi-shell-001:/usr/lib/tmpfiles.d]# 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


# Type=oneshot 这一选项适用于只执行一项任务、随后立即退出的服务
# 命令文件 /usr/bin/systemd-tmpfiles
# 命令参数 --clean
# 通过定期执行 /usr/bin/systemd-tmpfiles --clean 完成清理

命令: /usr/bin/systemd-tmpfiles

[root@shuidi-shell-001:/usr/lib/tmpfiles.d]# /usr/bin/systemd-tmpfiles --help
systemd-tmpfiles [OPTIONS...] [CONFIGURATION FILE...]

Creates, deletes and cleans up volatile and temporary files and directories.

  -h --help                 Show this help
     --version              Show package version
     --create               Create marked files/directories
     --clean                Clean up marked directories
     --remove               Remove marked files/directories
     --boot                 Execute actions only safe at boot
     --prefix=PATH          Only apply rules with the specified prefix
     --exclude-prefix=PATH  Ignore rules with the specified prefix
     --root=PATH            Operate on an alternate filesystem root


# --clean 将会清理被标记的文件目录

哪些目录被标记,又是什么样的标记呢?

定义在配置文件/usr/lib/tmpfiles.d/tmp.conf中

配置文件: /usr/lib/tmpfiles.d/tmp.conf

[root@shuidi-shell-001:/usr/lib/tmpfiles.d]# 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
#Type Path        Mode User Group Age Argument
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

x:在根据"寿命"字段清理过期文件时, 忽略指定的路径及该路径下的所有内容。 可以在"路径"字段中使用shell风格的通配符。 注意,这个保护措施对 r 与 R 无效。

X:在根据"寿命"字段清理过期文件时, 仅忽略指定的路径自身而不包括该路径下的其他内容。 可以在"路径"字段中使用shell风格的通配符。 注意,这个保护措施对 r 与 R 无效。

上述配置表示:

清理/tmp目录超过10天的内容,但是匹配/tmp/systemd-private-%b-*的目录及其路径下的全部内容会被保留
清理/var/tmp目录超过30天的内容,但是匹配/var/tmp/systemd-private-%b-*的目录及其路径下的全部内容被保留

参考链接:https://blog.csdn.net/weixin_42372875/article/details/80554115

tmpfiles.d 中文手册:http://www.jinbuguo.com/systemd/tmpfiles.d.html