关于/tmp目录的清理规则
關于/tmp目錄的清理規則
Sam
RHEL6
tmpwatch命令
tmpwatch 是專門用于解決“刪除 xxx天沒有被訪問/修改過的文件”這樣需求的命令。
安裝:
[root@sam01 ~]使用:
man tmpwatchtmpwatch - 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, --atimeMake the decision about deleting a file based on the file'satime (access time). This is the default.Note that the periodic updatedb file system scans keep theatime of directories recent.-m, --mtimeMake the decision about deleting a file based on the file'smtime (modification time) instead of the atime.-c, --ctimeMake the decision about deleting a file based on the file'sctime (inode change time) instead of the atime; for directo‐ries, make the decision based on the mtime.-M, --dirmtimeMake the decision about deleting a directory based on thedirectory's mtime (modification time) instead of the atime;completely ignore atime for directories.舉例:?(清除/tmp目錄下30天沒有被訪問文件)
[root@sam01 ~]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=指定的單元)最后一次被啟動的時間點上述配置文件表示兩種情況會執行該服務
服務如何執行呢?
[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-*/tmpx
在根據"壽命"字段清理過期文件時, 忽略指定的路徑及該路徑下的所有內容。 可以在"路徑"字段中使用shell風格的通配符。 注意,這個保護措施對 r 與 R 無效。
X
在根據"壽命"字段清理過期文件時, 僅忽略指定的路徑自身而不包括該路徑下的其他內容。 可以在"路徑"字段中使用shell風格的通配符。 注意,這個保護措施對 r 與 R 無效。
上述配置表示:
清理/tmp目錄超過10天的內容,但是匹配/tmp/systemd-private-%b-*的目錄及其路徑下的全部內容會被保留
清理/var/tmp目錄超過30天的內容,但是匹配/var/tmp/systemd-private-%b-*的目錄及其路徑下的全部內容被保留
總結
RHEL6 根據文件的訪問時間等條件使用tmpwatch命令進行/tmp目錄的清理,可以使用crond daemon進行定期執行
RHEL7 根據服務systemd-tmpfiles-clean.service 進行臨時文件的清理,清理規則定義在配置文件/usr/lib/tmpfiles.d/tmp.conf,調用命令為/usr/bin/systemd-tmpfiles --clean,執行時間依靠systemd-tmpfiles-clean.timer進行管理
總結
以上是生活随笔為你收集整理的关于/tmp目录的清理规则的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博途PID编程应用(状态机)
- 下一篇: 欢聚时代2017前端笔试C卷