频道栏目
首页 > 资讯 > 其他 > 正文

/tmp文件夹和/var/tmp文件夹的定期清理机制

18-06-27        来源:[db:作者]  
收藏   我要投稿

/tmp文件夹和/var/tmp文件夹的定期清理机制。

[root@hgdb02 tmp]# ll

total 8

drwxr-xr-x. 5 root root 37 May 19 11:01 benchmarksql-5.0 ---------->>>有此文件

drwxr-xr-x. 2 root root 6 Jun 8 10:10 hsperfdata_root

-rw-------. 1 root root 185 Jun 8 03:38 netdata-updater.log.6xMwv9

-rw-------. 1 root root 185 May 29 03:19 netdata-updater.log.BeFUtt

drwx------. 3 root root 16 Jun 25 15:20 systemd-private-28f9e88904ad483087853f5cade95232-cups.service-2sdeRq

---->>>>过一会,

[root@hgdb02 tmp]# cd ben ---------->>>无此文件

-bash: cd: ben: No such file or directory

[root@hgdb02 tmp]# cd ben*---------->>>无此文件

-bash: cd: ben*: No such file or directory

[root@hgdb02 tmp]# ll ---------->>>无此文件

total 0

drwx------. 3 root root 16 Jun 25 15:20 systemd-private-28f9e88904ad483087853f5cade95232-cups.service-2sdeRq

[root@hgdb02 tmp]#

我都怀疑这个OS是不是被黑了。。。。

然后查了一下/tmp下是否有定期清理的机制,还真有:

/tmp下的定期清理机制,在6和7下,是不一样的:

Oracle Linux: tmpfiles on Oracle Linux 7 (文档 ID 2273942.1) ---适用于linux 7

On Oracle Linux 7, tmpfiles cleans up /tmp or /var/tmp by removing unused files.

This function was called "tmpwatch" on OL6 which is invoked by crond, however, it is now realized by timer of systemd on OL7.

Files/Directories in /tmp Could be Removed Automatically (文档 ID 2212480.1) ----适用于linux 5和linux 6

如下全文转载Oracle Linux: tmpfiles on Oracle Linux 7 (文档 ID 2273942.1) :

On Oracle Linux 7, tmpfiles cleans up /tmp or /var/tmp by removing unused files.

This function was called "tmpwatch" on OL6 which is invoked by crond, however, it is now realized by timer of systemd on OL7.

Currently the detailed functions of tmpfiles are described in configuration files:

/usr/lib/systemd/system/systemd-tmpfiles-clean.timer

/usr/lib/systemd/system/systemd-tmpfiles-clean.service

/usr/lib/tmpfiles.d/tmp.conf

Shortly speaking, the functions are:

removing files/directories in /tmp/ un-accessed more than 10 days(defined in tmp.conf)

removing files/directories in /var/tmp/ un-accessed more than 30 days(defined in tmp.conf)

there are several files which will not be removed(defined in tmp.conf)

the removing command is "/usr/bin/systemd-tmpfiles --clean"(defined in systemd-tmpfiles-clean.service)

"un-accessed" is decided by checking all of atime/mtime/ctime of the file/directory.

Thus, in case even one of atime/mtime/ctime of a file in /tmp/ is newer than 10 days, the file will not removed.

If a file/directory in /tmp/ is not removed by tmpfiles even if it seems it is older than 10 days,

the reason can be checked by running a command with debug options like below manually:

# SYSTEMD_LOG_TARGET=console SYSTEMD_LOG_LEVEL=debug /usr/bin/systemd-tmpfiles --clean

For example, below intends that a directory "/tmp/abcdefg" can not be removed due to its atime.

# SYSTEMD_LOG_TARGET=console SYSTEMD_LOG_LEVEL=debug /usr/bin/systemd-tmpfiles --clean 2>&1 | grep abcdefg

Directory "/tmp/abcdefg": access time Wed 2017-05-24 16:56:28.771577 JST is too new

#

如下全文转载Files/Directories in /tmp Could be Removed Automatically (文档 ID 2212480.1)

APPLIES TO:

Linux OS - Version Oracle Linux 5.0 to Oracle Linux 6.8 [Release OL5 to OL6U8]

Information in this document applies to any platform.

SYMPTOMS

If files/directories in /tmp have not been accessed for a long time, they may be removed automatically.

CAUSE

"tmpwatch" cleans up the contents of /tmp or other temporary directories.

SOLUTION

In most cases, tmpwatch removing files is safely ignorable as unused files should be removed from /tmp to free up file system space so the file system containing /tmp doesn't fill over time.

However, if you strongly need to disable this function for some reason, despite the importance of tmpwatch in ensuring /tmp doesn't cause a file system to fill, it is possible by uninstalling tmpwatch:

# rpm -e tmpwatch

or, alternatively, move/remove /etc/cron.daily/tmpwatch file in case the rpm package for tmpwatch can't be removed, such as below:

# rpm -e tmpwatch

LANG=C rpm -e tmpwatch

error: Failed dependencies:

tmpwatch is needed by (installed) tetex-3.0-33.15.el5_8.1.x86_64

tmpwatch is needed by (installed) cups-1.3.7-32.el5_11.x86_64

#

 

Cleaning up unaccessed files in /tmp s a default function of package "tmpwatch", which provides a cron job /etc/cron.daily/tmpwatch. This is a shell script kicked by crond daily,which checks /tmp, /var/tmp and several directories in /var/ and remove files/directories which have not been accessed for a long time.

For example, /etc/cron.daily/tmpwatch on Oracle Linux 5 shows:

flags=-umc

/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \

-x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \

-X '/tmp/hsperfdata_*' 240 /tmp

/usr/sbin/tmpwatch "$flags" 720 /var/tmp

for d in /var/{cache/man,catman}/{cat,X11R6/cat,local/cat}; do

if [ -d "$d" ]; then

/usr/sbin/tmpwatch "$flags" -f 720 "$d"

fi

done

which will remove files/directories that have not been accessed for 240 or 720 hours(=10 or 30 days).

/etc/cron.daily/tmpwatch on Oracle Linux 6 shows:

#! /bin/sh

flags=-umc

/usr/sbin/tmpwatch "$flags" -x /tmp/.X11-unix -x /tmp/.XIM-unix \

-x /tmp/.font-unix -x /tmp/.ICE-unix -x /tmp/.Test-unix \

-X '/tmp/hsperfdata_*' -X '/tmp/.hdb*lock' -X '/tmp/.sapstartsrv*.log' \

-X '/tmp/pymp-*' 10d /tmp

/usr/sbin/tmpwatch "$flags" 30d /var/tmp

for d in /var/{cache/man,catman}/{cat,X11R6/cat,local/cat}; do

if [ -d "$d" ]; then

/usr/sbin/tmpwatch "$flags" -f 30d "$d"

fi

done

which will remove files/directories that have not been accessed for 10 or 30 days.

相关TAG标签
上一篇:Ubuntu下查看和修改hostname
下一篇:linux kernel bug导致无法载入nvidia驱动问题的解决办法
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站