Centos7清理/tmp文件夹
Centos 6以及6以下的系统使用watchtmp + cron来实现定时清理临时文件的效果,这点在Centos 7发生了变化。
一. 系统自动清理
在Centos 7下,系统使用systemd管理易变与临时文件,/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目录文件,原理就是使用了tmpwatch。
首先进行安装:yum install -y tmpwatch
安装成功后,执行清理任务:tmpwatch 24 /tmp/ #删除/tmp目录下超过24小时未使用的文件,最小为1小时。