略微加速

略速 - 互联网笔记

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

2020-06-09 leiting (3345阅读)

标签 Linux 运维

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

systemd-tmpfiles-setup.service  :Create Volatile Files and Directories
systemd-tmpfiles-setup-dev.service:Create static device nodes in /dev
systemd-tmpfiles-clean.service :Cleanup of Temporary Directories

相关的配置文件也有3个地方:

/etc/tmpfiles.d/*.conf
/run/tmpfiles.d/*.conf
/usr/lib/tmpfiles.d/*.conf

/tmp目录的清理规则主要取决于/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           #   清理/tmp下10天前的目录和文件
v /var/tmp 1777 root root 30d       #   清理/var/tmp下30天前的目录和文件

# 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

我们可以配置这个文件,比如你不想让系统自动清理/tmp下以tomcat开头的目录,那么增加下面这条内容到配置文件中即可:

x /tmp/tomcat.*


===================

启动systemd-tmpfiles-clean.service:

systemctl start systemd-tmpfiles-clean.service

systemd-tmpfiles-clean.service由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天后执行服务

也可以重启systemd-tmpfiles-clean.timer服务

===================

systemctl命令的使用及服务状态的查看

一、systemd


系统初始化程序,系统开始的第一个进程,pid为1



二、systemctl命令


systemctl list-units            ##列出当前系统服务的状态


systemctl list-unit-files       ##列出服务的开机状态


systemctl status sshd           ##查看指定服务的状态


systemctl stop sshd             ##关闭指定服务


systemctl start sshd            ##开启指定服务


systemctl restart sshd          ##从新启动服务


systemctl enable sshd           ##设定指定服务开机开启


systemctl disable sshd          ##设定指定服务开机关闭


systemctl reload sshd           ##使指定服务从新加载配置


systemctl list-dependencies sshd    ##查看指定服务的倚赖关系


systemctl mask  sshd            ##冻结指定服务


systemctl unmask sshd           ##启用服务


systemctl set-default multi-user.target ##开机不开启图形


systemctl set-default graphical.target  ##开机启动图形


setterm                 ##文本界面设定color


三、服务状态


systemctl   status     服务名称


loaded             ##系统服务已经初始化完成,加载过配置


active(running)       ##正有一个或多个程序正在系统中执行, vsftpd就是这种模式


atcive(exited)        ##僅執行一次就正常結束的服務, 目前並沒有任何程序在系統中執行


atcive(waiting)       ##正在執行當中,不過還再等待其他的事件才能继续处理


inactive            ##服务关闭


enbaled           ##服务开机启动


disabled          ##服务开机不自启


static                ##服务开机启动项不可被管理


failed                ##系统配置错误



参考:

https://blog.csdn.net/leonnew/article/details/79258453

https://blog.csdn.net/weixin_42372875/article/details/80554115

http://www.jinbuguo.com/systemd/tmpfiles.d.html


北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3