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


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

 

RHEL6

tmpwatch命令

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

 

安裝:

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

 

使用:

man tmpwatch

tmpwatch  -  removes files which haven't 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  file's
          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  file's
          mtime (modification time) instead of the atime.

   -c, --ctime
          Make  the decision about deleting a file based on the file's
          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
          directory's 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進行管理

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM