How is the /tmp directory cleaned up?
Note! This answer is outdated since at least ubuntu 14.04. See other answers for current situation and if they prove correct then upvote them furiously. Also post comment so I can put link here to current correct answer.
For 14.04 see https://askubuntu.com/a/759048/1366
For 16.10 see https://askubuntu.com/a/857154/453746
Old answer from 2011:
The cleaning of /tmp
is done by the upstart script /etc/init/mounted-tmp.conf
. The script is run by upstart everytime /tmp
is mounted. Practically that means at every boot.
The script does roughly the following: if a file in /tmp
is older than $TMPTIME
days it will be deleted.
The default value of $TMPTIME
is 0, which means every file and directory in /tmp
gets deleted. $TMPTIME
is an environment variable defined in /etc/default/rcS
.
The directory is cleared by default at every boot, because TMPTIME
is 0 by default.
Here you can change the time in the following file:
/etc/default/rcS
TMPTIME
says how frequent the tmp dir sould be cleared in days
While the /tmp
folder is not a place to store files long-term, occasionally you want to keep things a little longer than the next time you reboot, which is the default on Ubuntu systems. I know a time or two I’ve downloaded something to /tmp
during testing, rebooted after making changes and then lost the original data again. This can be changed if you’d like to keep your /tmp
files a little bit longer.
Changing the /tmp
Cleanup Frequency
The default setting that tells your system to clear /tmp
at reboot is held in the /etc/default/rcS
file. The value we’ll look at is TMPTIME
.
The current value of TMPTIME=0
says delete files at reboot despite the age of the file. Changing this value to a different (positive) number will change the number of days a file can survive in /tmp
.
TMPTIME=7
This setting would allow files to stay in /tmp
until they are a week old, and then delete them on the next reboot. A negative number (TMPTIME=-1
) tells the system to never delete anything in /tmp
. This is probably not something you want, but is available.