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

本文介绍了Oracle Linux 5到7中/tmp和/var/tmp文件夹的自动清理机制,包括tmpwatch和systemd定时任务的配置与原理,以及如何禁用这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



/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.

参考文章:

https://developers.redhat.com/blog/2016/09/20/managing-temporary-files-with-systemd-tmpfiles-on-rhel7/

根据提供的引用内容,这些引用是关于清理临时文件的脚本。这些脚本使用tmpwatch命令来删除一定时间内没有被访问的文件目录。根据不同的配置,文件目录的保留时间可以是240小时(10天)或720小时(30天)\[2\]。至于你提到的"/tmp/phplKVBOf",根据提供的信息,无法确定它是一个文件还是一个目录。如果它是一个文件,并且它在清理脚本的保留时间范围内没有被访问,那么它可能会被清理掉。如果它是一个目录,那么它的子文件目录可能会根据相同的规则进行清理。请注意,这只是一个推测,具体的清理行为可能会根据实际的配置环境而有所不同。 #### 引用[.reference_title] - *1* *2* [/tmp文件夹/var/tmp文件夹定期清理机制](https://blog.youkuaiyun.com/msdnchina/article/details/80813810)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Linux临时目录/tmp与/var/tmp](https://blog.youkuaiyun.com/skyupward/article/details/107809142)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值