I'm taking to putting various files in /tmp
, and I wondered about the rules on deleting them?
I'm imagining it's different for different distributions, and I'm particularly interested in Ubuntu and Fedora desktop versions.
But a nice general way of finding out would be a great thing.
Even better would be a nice general way of controlling it! (Something like 'every day at 3 in the morning, delete any /tmp
files older than 60 days, but don't clear the directory on reboot')
That depends on your distribution. On some system, it's deleted only when booted, others have cronjobs running deleting items older than n hours.
tmpreaper
which gets called by/etc/cron.daily
, configured via/etc/default/rcS
and/etc/tmpreaper.conf
. (Credits to this answer).tmpfiles.d
. (Credits to this answer)./etc/default/rcS
)./etc/cron.daily/tmpwatch
; RHEL7/RHEL8 and RedHat-like with systemd it's configured in/usr/lib/tmpfiles.d/tmp.conf
, called bysystemd-tmpfiles-clean.service
)./etc/conf.d/bootmisc
.On CentOS (and I assume Fedora), there's a job in /etc/cron.daily called
tmpwatch
. This runs/usr/sbin/tmpwatch
, which will delete files that haven't been accessed in the specified number of hours, i.e., the default behavior is to examine theatime
for the file to evaluate if it's been used recently.http://linux.die.net/man/8/tmpwatch
Other distros (and installations) may have /tmp mounted as tmpfs, which is an in-memory filesystem. This will get cleared on boot.
On Ubuntu 11.10 which I'm using, there's an upstart script in
/etc/init/mounted-tmp.conf
. The start of it says this:You can read in more details, however in general
/tmp
is cleaned when it's either mounted or/usr
is mounted. This regularly happens on boot, so this/tmp
cleaning runs on every boot.In
/etc/default/rcS
you haveTMPTIME
set, which is used in the above init script to feed the twofind
commands at its end - basically controlling file deletion based on their times (modified, changed, accessed).From Fedora 18 on,
/tmp
is mounted ontmpfs
(i.e. RAM) by default, and thus erased on power off.This behaviour can be disabled by issuing
systemctl mask tmp.mount
and reboot (and reenabled by issuingsystemctl unmask tmp.mount
and reboot), and then/tmp
will be mounted on the/
filesystem and can be controlled by/usr/lib/tmpfiles.d/tmp.conf
settings.See http://fedoraproject.org/wiki/Features/tmp-on-tmpfs and
man tmpfiles.d
for more details on each case.On RHEL 6.2 the files in /tmp are deleted by tmpwatch if they have not been accessed in 10 days.
The file /etc/cron.daily/tmpwatch defines the way tmpwatch is called.
The -x arguments are files to be excluded. The 2nd to last argument is the time to wait after the last accessed time. The last argument is the directory to examine.
Sorta-tempy files that you do not want cleared on boot go in /var/tmp
That's what it's for :-)
On openSUSE 13.2, the clearing behaviour could be controlled with the following variable in sysconfig.
MAX_DAYS_IN_TMP
MAX_DAYS_IN_LONG_TMP
TMP_DIRS_TO_CLEAR
LONG_TMP_DIRS_TO_CLEAR
OWNER_TO_KEEP_IN_TMP
CLEAR_TMP_DIRS_AT_BOOTUP
You could modified these variables by (each variable's usage could also be found there)
/etc/sysconfig/cron
file manually in command line.In addition to all the detailed answers found here, it may be interesting to note what the FHS (Filesystem Hierarchy Standard) has to say about the temporary directories in general:
/tmp
/var/tmp