Linux下关于/tmp目录的清理规则

本文将介绍Linux下/tmp目录的清理规则,rhel6和rhel7将以完全不同的两种方式进行清理。

RHEL6

tmpwatch命令

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

安装:

[[email protected] ~]# 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天没有被访问文件)

[[email protected] ~]# tmpwatch --atime 30d /tmp

RHEL7

systemd-tmpfiles-clean.service服务

服务: systemd-tmpfiles-clean.service

服务何时被执行呢?

Linux下该服务的执行可以根据systemd-tmpfiles-clean.timer进行管理

[[email protected] ~]# 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天后执行服务

服务如何执行呢?

[[email protected] ~]# 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

[[email protected] ~]# /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

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

原文地址:https://www.cnblogs.com/samtech/p/9490166.html

时间: 2024-08-29 16:46:49

Linux下关于/tmp目录的清理规则的相关文章

解决Linux下Tomcat日志目录下的catalina.log日志文件过大的问题

本文摘自:(http://blog.csdn.net/stevencn76/article/details/6246162) 分类: Java技术专区2011-03-13 12:25 5017人阅读 评论(1) 收藏 举报 tomcatlinux工具任务web 由于Tomcat在默认情况下会将没有经过配置的web应用所产生的日志输出已经其本身的日志内容都输出到这个文件中,那么随着时间的推移,这个文件的尺寸将会越来越大,当需要检查日志内容时间会导致文件难以打开,而且同时tomcat依旧在不断的向文

【转帖】linux下的各个目录的含义

linux下的各个目录的含义 http://embeddedlinux.org.cn/emb-linux/entry-level/200809/22-85.html/bin/usr/local/bin/usr/bin 三个目录不一样 不过发现unix 还是很喜欢讲软件安装在 /opt 目录下面的. 路径名                        内容/bin                    为得到最小的系统操作性所需要的那些命令/boot                   内核和加

如何在Linux下拷贝一个目录呢

cp -af newadmin/movie/.   uploadfile/mallvideo/ 如何在Linux下拷贝一个目录呢?这好像是再简单不过的问题了. 比如要把/home/usera拷贝到/mnt/temp,首先想到的就是 cp -R /home/usera/* /mnt/temp 但是这样有一个问题,/home/usera下的隐藏文件都不会被拷贝,子目录下的隐藏文件倒是会的. 那如何才是正确的方法呢?有人说用-a选项,有人说用find加管道. 其实没这么复杂,Google了之后,学了一

Linux下ThinkPHP网站目录权限设置

在windows上运行好好的项目,迁移到Linux上就遇到了很多问题,其中最为重要的是网站目录权限的设置,当然简单期间你可以用 命令 "chmod 777 -R you web site" ,这样你就不用担心项目运行的时候会涉及到权限问题.通常来说如果你的项目时部署在公网或者是部 署在租用的Linux服务器上不建议这么做,应为如果这样设置了那么任何用户都可以对这个站点下的文件,目录进行操作,这样也会影响网站的运营, 同样不安全.下面以ubuntu14.4 系统为主进行分析. 最简单的做

linux下文件和目录的属性

linux下文件或目录的属性 [[email protected] ~]# ls -l -rw-r--r--. 1 root root      9119 Nov 13 09:29 install.log drwxr-xr-x. 2 root root      4096 Mar 17 13:50 test #列出当前所有的目录 ^d代表以d开头的类型 [[email protected] ~]# ls -l |grep '^d'     drwxr-xr-x. 2 root root     

Windows访问Linux下的共享目录的配置方法

user安全级别 第一步:安装samba3(如果已经安装就跳过这一步) [[email protected] /]# yum groupinstall "CIFS file server" 第二步:修改配置文件 [[email protected] /]# vi /etc/samba/smb.conf 将security参数的值设为user(这是默认值) security = user 第三步:创建可以访问共享目录的用户 [[email protected] test]# userad

linux下删除某个目录下大量文件办法

在lamp架构里,因为设置session 超时时间为一天,所以遇到php产生的session大量存在/data/tmp目录下,数量大概有200万,并且不能及时回收.使用 rm -fr /data/tmp/sess_* 不能删除,rm命令会提示参数太多. 后,使用rsync同步删除命令实现,完美实现了批量删除大量垃圾文件操作.具体步骤如下: 大量文件存在的目录:/data/tmp 使用命令: #cd /data#mkdir -pv /data/null # 建立临时空目录 #rsync -proc

linux下查找某个目录下包含某个字符串的文件

有时候要找一些字符串,但是又不知道在哪个文件,只记得一些字符串 那么如何在linux下寻找包含某段文字的文件呢? 强大的find命令可以帮你完成不可能的任务. 比如我只记得我的程序里包含唯一的字符串“SMS_ISSEND”,于是: 查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri “SMS_ISSEND” 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xargs grep -ri “SMS_ISSEND” -l

linux下的缓存机制及清理buffer/cache/swap的方法梳理

(1)缓存机制 为了提高文件系统性能,内核利用一部分物理内存分配出缓冲区,用于缓存系统操作和数据文件,当内核收到读写的请求时,内核先去缓存区找是否有请求的数据,有就直接返回,如果没有则通过驱动程序直接操作磁盘. 缓存机制优点:减少系统调用次数,降低CPU上下文切换和磁盘访问频率. CPU上下文切换:CPU给每个进程一定的服务时间,当时间片用完后,内核从正在运行的进程中收回处理器,同时把进程当前运行状态保存下来,然后加载下一个任务,这个过程叫做上下文切换.实质上就是被终止运行进程与待运行进程的进程