Linux运行环境:
[root@foundation0 RHCE]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.0 (Maipo)
[root@foundation0 RHCE]# uname -a
Linux foundation0.ilt.example.com 3.10.0-123.el7.x86_64 #1 SMP Mon May 5 11:16:57 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
服务:systemd-tmpfiles-setup
运行命令systemd-tmpfile --create --remove
以上命令会从以下目录下读取配置文件:
/usr/lib/tmpfiles.d/*.conf #软件运行 优先级最低
/run/tmpfiles.d/* #内存运行
/etc/tmpfiles.d/* #配置运行 优先级最高
作用:系统会删除这些配置文件标记要删除的任何文件和目录,并且会创建标记要创建(或修复权限)的任何文件和目录,并使其拥有正确的权限
systemd定时器会定间隔调用systemd-tmpfiles --clean 来清理临时文件
[root@foundation0 ~]# systemctl list-unit-files --type=service | grep systemd-tmpfiles
systemd-tmpfiles-clean.service static
systemd-tmpfiles-clean.timer配置文件
OnBootSec=15min
OnUnitActiveSec=1d
以上配置文件表示同名服务(systemd-tmpfiles-clean.service)将在systemd启动15分钟后启动,然后每隔24小时启动一次
Systemd-tmpfiles配置文件格式:
类型 路径 模式 UID GID 期限和参数
d :创建不存在的文件或目录
- :表示不清除
L 创建指定文件的链接
-D:如果目录不存在则创建目录,如果目录存在则清空其所有的内容
d /run/systemd /setts 0755 root root - #创建目录/run/systemd/setts 并禁止删除这个文件
L /run/fstablink - root root - /etc/fstab #创建指向/etc/fstab的连接
[root@foundation0 tmpfiles.d]# pwd
/etc/tmpfiles.d
[root@foundation0 tmpfiles.d]# ls
libstoragemgmt.conf named.conf radvd.conf tmp.conf tuned.conf
[root@foundation0 tmpfiles.d]# cat named.conf
d /run/named 0755 named named - #创建文件/run/named,并且不能删除
[root@foundation0 tmpfiles.d]# cat 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
d /tmp 1777 root root 10d #10天没有修改的文件会删除
d /var/tmp 1777 root root 30d #30天没有修改的文件会删除
[root@foundation0 tmpfiles.d]# pwd
/etc/tmpfiles.d
[root@foundation0 tmpfiles.d]# ls
libstoragemgmt.conf named.conf radvd.conf tmp.conf tuned.conf
[root@foundation0 tmpfiles.d]# systemd-tmpfiles --clean tmp.conf
测试:
创建一个新配置文件在/etc/tmpfiles.d目录下
[root@foundation0 tmpfiles.d]# pwd
/etc/tmpfiles.d
[root@foundation0 tmpfiles.d]# cat gallifrey.conf
d /run/gallifrey 0700 root root 10s #在/run目录下创建一个目录gallifrey,这个目录中的文件在创建10s后删除
运用配置文件,使配置文件生效,创建/run/gallifrey目录
[root@foundation0 tmpfiles.d]# systemd-tmpfiles --create gallifrey.conf
查看目录是否创建:
[root@foundation0 tmpfiles.d]# ls -dl /run/gallifrey/
drwx------. 2 root root 40 Nov 26 20:50 /run/gallifrey/
在/run/gallifrey目录下创建文本文件:
[root@foundation0 gallifrey]# for i in {1..20};do touch $i.txt ;done
[root@foundation0 gallifrey]# ls
10.txt 12.txt 14.txt 16.txt 18.txt 1.txt 2.txt 4.txt 6.txt 8.txt
11.txt 13.txt 15.txt 17.txt 19.txt 20.txt 3.txt 5.txt 7.txt 9.txt
手动应用配置文件:
[root@foundation0 gallifrey]# systemd-tmpfiles --clean /etc/tmpfiles.d/gallifrey.conf
[root@foundation0 gallifrey]# ls
[root@foundation0 gallifrey]# pwd
/run/gallifrey
在等待30s后,再次查看/run/gallifrey目录下的文件:
[root@foundation0 gallifrey]# pwd
/run/gallifrey
[root@foundation0 gallifrey]# ls -al
total 0
drwx------. 2 root root 40 Nov 26 21:00 .
drwxr-xr-x. 39 root root 1200 Nov 26 20:47 ..
这个目录下的文件已全部被删除了