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() man:systemd-tmpfiles() [Timer]
OnBootSec=15min
OnUnitActiveSec=1d # OnBootSec 表示相对于机器被启动的时间点
# 表示相对于匹配单元(本标签下Unit=指定的单元)最后一次被启动的时间点
上述配置文件表示两种情况会执行该服务
- 开机15分钟执行服务
- 距离上次执行该服务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() man:systemd-tmpfiles()
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() for details # Clear tmp directories separately, to make them easier to override
v /tmp root root 10d
v /var/tmp 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-*的目录及其路径下的全部内容被保留
总结
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进行管理
Linux下关于/tmp目录的清理规则的更多相关文章
- CentOS7的/tmp目录自动清理规则
CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化. 在CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系 ...
- CentOS7的/tmp目录自动清理规则(转)
CentOS7的/tmp目录自动清理规则 CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用sys ...
- 【转帖】linux下的各个目录的含义
linux下的各个目录的含义 http://embeddedlinux.org.cn/emb-linux/entry-level/200809/22-85.html/bin/usr/local/bin ...
- 如何在Linux下拷贝一个目录呢
cp -af newadmin/movie/. uploadfile/mallvideo/ 如何在Linux下拷贝一个目录呢?这好像是再简单不过的问题了. 比如要把/home/usera拷贝到/m ...
- linux下递归列出目录下的所有文件名(不包括目录)
1.linux下递归列出目录下的所有文件名(不包括目录) ls -lR |grep -v ^d|awk '{print $9}'2.linux下递归列出目录下的所有文件名(不包括目录),并且去掉空行 ...
- 怎么查这个文件在linux下的哪个目录
因为要装pl/sql所以要查找tnsnames.ora文件..看看怎么查这个文件在linux下的哪个目录 find / -name tnsnames.ora 查到: /opt/app/oracle/p ...
- Linux tmp目录自动清理总结
在Linux系统中/tmp文件夹下的文件是会被清理.删除的,文件清理的规则是如何设定的呢? 以Redhat为例,这个主要是因为作业里面会调用tmpwatch命令删除那些一段时间没有访问的文件. 那么什 ...
- Linux 下执行本目录的可执行文件(命令)为什么需要在文件名前加“./”
一.PATH 是环境变量,里面保存了执行文件路径(通常会包含多个路径,各路径之间以冒号":"进行间隔).当执行一个可执行文件(命令)时,Linux 会优先到 PATH 环境变量中保 ...
- Linux下将/TMP和/Var移动到共享分区
2007-03-09 03:25:08 整理数据 首先,必须创建一个新分区专门用于存储频繁修改的文件.您可能希望将这个分区置于单独的磁盘上以增强性能.接下来,我将逐步说明将 /tmp 和 /va ...
随机推荐
- 浅谈Nginx服务器的内部核心架构设计
前言 Nginx 是一个 免费的 , 开源的 , 高性能 的 HTTP 服务器和 反向代理 ,以及 IMAP / POP3代理服务器. Nginx 以其高性能,稳定性,丰富的功能,简单的配置和低资源消 ...
- ADO学途 two day
代码实现的参照性在学习程序中占了关键比重,最基本的都一直无法运行成功,那就无法深入 研究.实现winfrom功能的要点之一实践中获取原理:不清楚代码的一些原理,即使copy过来,大多也 存无法运行的情 ...
- Django (五) modeld进阶
day 05 models进阶 1.models基本操作 django中遵循 Code Frist 的原则,即:根据代码中定义的类来自动生成数据库表. 对于ORM框架里: 我们写的类表示数据库的表 ...
- EM算法(徐亦达)笔记
- 解决windows下 Python中 matplotlib 做图中文不显示的问题
在代码中填入以下两句即可 from pylab import mpl mpl.rcParams['font.sans-serif'] = [font_name] 如:mpl.rcParams['fon ...
- 1049 - Deg-route
http://www.ifrog.cc/acm/problem/1049 这些数学题我一般都是找规律的.. 先暴力模拟了前面的那些,然后发现(x, y) = (x, y - 1) + (x - 1, ...
- APACHE服务器httpd.exe进程占用cpu100%的解决方法
httpd.exe进程占用cpu%100,关闭掉AppServ服务,cpu应用率立刻下降到0. 重新启动AppServ又出现占用cpu高的情况. 原因,httpd.exe和防火墙配置有冲突. 解决方法 ...
- 基于Servlet+smartUpload的文件上传
文件上传在web应用中是非常常见的,现在我就介绍下基于servlet的文件上传,基于Struts2的文件上传可以看: 页面端代码: <%@ page language="java&qu ...
- UVA 10572 Black & White (状压DP)
题意:有一个n*m的矩阵,其中部分格子已经涂黑,部分涂白,要求为其他格子也上黑/白色,问有多少种涂法可以满足一下要求: (1)任意2*2的子矩阵不可以同色. (2)所有格子必须上色. (3)只能有两个 ...
- 目后佐道IT教育的品牌故事
关于目后佐道 目后佐道IT教育作为中国IT职业教育领导品牌,致力于HTML5.UI.PHP.Java+大数据.Python+人工智能.Linux.产品经理.测试.运维等课程培训.100%全程面授,平均 ...