Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm taking to putting various files in /tmp, and I wondered what the rules on deleting them are?

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')

share|improve this question

5 Answers 5

up vote 29 down vote accepted

That depends on your distribution. On some system, it's deleted only when booted, others have cronjobs running deleting items older than n hours.

On Debian-like systems, the rules are defined in /etc/default/rcS. RedHat-like, it's /etc/cron.daily/tmpwatch and /etc/conf.d/bootmisc for Gentoo.

share|improve this answer
6  
And regardless of when this happens, the only safe moment is generally on boot, right after mounting it, since running processes may have files locked there, and these should not be deleted. –  adaptr Apr 6 '12 at 15:14
    
There are also packages available that can remove them periodically. –  Samuel Edwin Ward Apr 6 '12 at 20:15

From Fedora 18 on, /tmp is mounted on tmpfs (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 issuing systemctl 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.

share|improve this answer

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.

#! /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_*' 10d /tmp

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.

share|improve this answer

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 the atime 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.

share|improve this answer

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:

# mounted-tmp - Clean /tmp directory
#
# Cleans up the /tmp directory when it does not exist as a temporary
# filesystem.

description "Clean /tmp directory"

start on (mounted MOUNTPOINT=/tmp) or (mounted MOUNTPOINT=/usr)

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 have TMPTIME set, which is used in the above init script to feed the two find commands at its end - basically controlling file deletion based on their times (modified, changed, accessed).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.