关于/tmp目录的清理规则

Sam

RHEL6

tmpwatch命令

tmpwatch 是专门用于解决“删除 xxx天没有被访问/修改过的文件”这样需求的命令。

安装:

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

使用:

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 ~]# 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进行管理

关于/tmp目录的清理规则相关推荐

  1. linux centos7 /tmp目录 自动清理规则

    CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系统服务有3个: systemd-tmpfiles-setup.service :Create Volatile Files an ...

  2. centos7 tmp目录 自动清理规则

    CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系统 ...

  3. CentOS7的/tmp目录自动清理规则(转)

    CentOS7的/tmp目录自动清理规则 CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用sys ...

  4. 所有的service报红但不报错_从一个应用报错来看centos系统的/tmp目录自动清理规则...

    概述 分享最近应用碰到的一个奇怪bug,一开始以为是代码上的问题,找了一段时间发现居然是因为系统的一个自动清理规则导致,下面一起来看看吧~ 一.应用报错: logwire.core.exception ...

  5. CentOS7的/tmp目录自动清理规则

    CentOS6以下系统(含)使用watchtmp + cron来实现定时清理临时文件的效果,这点在CentOS7发生了变化,在CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系统 ...

  6. linux tmp 不自动清理,Linux tmp目录自动清理总结

    在Linux系统中/tmp文件夹下的文件是会被清理.删除的,文件清理的规则是如何设定的呢? 以Redhat为例,这个主要是因为作业里面会调用tmpwatch命令删除那些一段时间没有访问的文件. 那么什 ...

  7. CentOS7 /tmp目录自动清理

    CentOS7下,系统使用systemd管理易变与临时文件,与之相关的系统服务有3个: systemd-tmpfiles-setup.service :Create Volatile Files an ...

  8. tmp ubuntu 自动删除吗_Linux tmp目录自动清理总结

    在Linux系统中/tmp文件夹下的文件是会被清理.删除的,文件清理的规则是如何设定的呢? 以Redhat为例,这个主要是因为作业里面会调用tmpwatch命令删除那些一段时间没有访问的文件. 那么什 ...

  9. Linux tmp目录自动清理 及解决方法

    在Linux系统中/tmp文件夹下的文件是会被清理.删除的,文件清理的规则是如何设定的呢? 在root/tmp文件下 -rw-rw-r-- 1 hadoop hadoop 5 Feb 16 20:56 ...

最新文章

  1. 面试必备:4种经典限流算法讲解
  2. 关于文章 Generating Impact-Based Summaries... By Mei qiaozhu
  3. 新设备关联Gitlab
  4. 利用who,w,ps和top等指令查看linux下的进程执行情况.,UNIXLINUX操作系统实验指导书...
  5. Qt Creator开放项目
  6. 在Linux虚拟机中添加多个ip地址
  7. php 获取301 302的真实地址
  8. 树莓派python_树莓派中如何创建python文件
  9. [css] 举例说明你知道的css技巧有哪些?
  10. 基因组浏览器使用 (EPGG)
  11. php mssql_init,Php Mssql操作简单封装支持存储过程
  12. 注册(五)之请求处理
  13. iPhone SE第三代强势入局后,我们来谈谈iPhoneSE2020
  14. 求助动态贝叶斯网络参数学习函数的使用方法
  15. 校园网下,虚拟机IP与主机IP不一致及nfs挂载
  16. 04zookeeper场景应用-master选举
  17. 傻瓜式教学——手把手教你电脑三种方式连接打印机
  18. 低调的大神!他改变了半导体产业!史上唯一两次获得诺贝尔物理奖,却几乎被人遗忘...
  19. 【阅读总结】Xen and the Art of Virtualization
  20. 2018年最新Python Flask打造一个视频网站实战视频教程分享

热门文章

  1. python隐藏数据库密码忘了怎么办_当你忘记网站上的密码时怎么办?Python如何快速帮你找回?...
  2. 浅析无符号整型和有符号整型
  3. 计算机原理董洁答案,微计算机原理
  4. 虚拟内存-什么是虚拟内存
  5. PHP自定义生成html网页
  6. C++经典问题:狐狸找兔子
  7. MVC:用bShare插件分享内容至QQ空间
  8. adb shell dumpsys alarm 显示所有Alarm
  9. 程序员在古代相当于什么职业?
  10. 【第二十三篇】Spring Boot集成redis