1. 程式人生 > >Linux下關於/tmp目錄的清理規則

Linux下關於/tmp目錄的清理規則

atime direct res fine single distrib his ystemd moved

本文將介紹Linux下/tmp目錄的清理規則,rhel6和rhel7將以完全不同的兩種方式進行清理。

RHEL6

tmpwatch命令

tmpwatch 是專門用於解決“刪除 xxx天沒有被訪問/修改過的文件”這樣需求的命令。

安裝:

[root@sam01 ~]# yum install tmpwatch.x86_64

使用:

man tmpwatch

tmpwatch  -  removes files which havent been accessed for a period of time.

By  default, tmpwatch dates files by their atime (access 
time), not their mtime (modification time). The time parameter defines the threshold for removing files. If the file has not been accessed for time, the file is removed. The time argument is a number with an optional single-character suffix specifying the units: m for minutes, h for hours, d for
days. If no suffix is specified, time is in hours. -u, --atime Make the decision about deleting a file based on the files atime (access time). This is the default. Note that the periodic updatedb file system scans keep the atime of directories recent.
-m, --mtime Make the decision about deleting a file based on the files mtime (modification time) instead of the atime. -c, --ctime Make the decision about deleting a file based on the files ctime (inode change time) instead of the atime; for directo‐ ries, make the decision based on the mtime. -M, --dirmtime Make the decision about deleting a directory based on the directorys mtime (modification time) instead of the atime; completely ignore atime for directories.

舉例: (清除/tmp目錄下30天沒有被訪問文件)

[root@sam01 ~]# tmpwatch --atime 30d /tmp

RHEL7

systemd-tmpfiles-clean.service服務

服務: systemd-tmpfiles-clean.service

服務何時被執行呢?

Linux下該服務的執行可以根據systemd-tmpfiles-clean.timer進行管理

[root@sam01 ~]# 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@sam01 ~]# 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@sam01 ~]# /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@sam01 ~]# 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

x 在根據
"壽命"字段清理過期文件時, 忽略指定的路徑及該路徑下的所有內容。 可以在"路徑"字段中使用shell風格的通配符。 註意,這個保護措施對 r 與 R 無效。 X 在根據"壽命"字段清理過期文件時, 僅忽略指定的路徑自身而不包括該路徑下的其他內容。 可以在"路徑"字段中使用shell風格的通配符。 註意,這個保護措施對 r 與 R 無效。

上述配置表示:

  1. 清理/tmp目錄超過10天的內容,但是匹配/tmp/systemd-private-%b-*的目錄及其路徑下的全部內容會被保留

  2. 清理/var/tmp目錄超過30天的內容,但是匹配/var/tmp/systemd-private-%b-*的目錄及其路徑下的全部內容被保留

總結

  1. RHEL6 根據文件的訪問時間等條件使用tmpwatch命令進行/tmp目錄的清理,可以使用crond daemon進行定期執行

  2. RHEL7 根據服務systemd-tmpfiles-clean.service 進行臨時文件的清理,清理規則定義在配置文件/usr/lib/tmpfiles.d/tmp.conf,調用命令為/usr/bin/systemd-tmpfiles --clean,執行時間依靠systemd-tmpfiles-clean.timer進行管理

 

Linux下關於/tmp目錄的清理規則