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.
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.
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.
I'm checking this on Ubuntu 16.10. I can certify that editing /etc/default/rcS has no effect at all anymore and the files in tmp are wiped out by reboot no matter what you put in that file. As others mention, tmpreaper is no longer used.
I think the right answer is that Ubuntu 16.10 has a new setup. There is a folder /etc/tmpfiles.d, documented in the man page "tmpfiles.d". In that folder, one should place a configuration file to control whether the /tmp is to be erased. This is what I am doing to stop reboots from erasing files in /tmp unless they are 20 days old:
#/etc/tmpfiles.d/tmp.conf
d /tmp 1777 root root 20d
Replace "20d" by "-" if you never want files deleted. This is my best effort, that man page is nearly impenetrable with detail.
The advantage of the new setup is that a file cleaner can still run even if the system is not rebooted (as in the case of an always on server). That's a big plus, I think.
In Ubuntu 14.04 this is done by tmpreaper, which is called daily by cron (from /etc/cron.daily). The program can be configured via /etc/default/rcS and /etc/tmpreaper.conf.
In a systemd Ubuntu (15.10 and newer), this is done by systemd, using the systemd-tmpfiles-clean service and timer:
$ systemctl cat systemd-tmpfiles-clean.service
# /lib/systemd/system/systemd-tmpfiles-clean.service
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=local-fs.target time-sync.target
Before=shutdown.target
[Service]
Type=oneshot
ExecStart=/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle
And
$ systemctl cat systemd-tmpfiles-clean.timer
# /lib/systemd/system/systemd-tmpfiles-clean.timer
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
[Timer]
OnBootSec=15min
OnUnitActiveSec=1d
You can change the timer behaviour itself using systemctl edit systemd-tmpfiles-clean.timer, and using various systemd Timer configuration options (see man 5 systemd.timer).
So systemd-tmpfiles-clean runs on shutdown, and once per day otherwise. The files it cleans can be extended using /etc/tmpfiles.d (mentioned in another answer).
It's interesting to note that as default /etc/tmpfiles.d is empty. The file where the /tmp policy is defined is here:
/usr/lib/tmpfiles.d/tmp.conf
Actual content:
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# See tmpfiles.d(5) for details
# Clear tmp directories separately, to make them easier to override
D /tmp 1777 root root -
#q /var/tmp 1777 root root 30d
On one of our servers running Ubuntu, we have a script to remove files in /tmp and it runs nightly.
The script is:
#!/bin/sh
# Clean file and dirs more than 3 days old in /tmp nightly
/usr/bin/find /tmp -type f -atime +2 -mtime +2 |xargs /bin/rm -f &&
/usr/bin/find /tmp -type d -mtime +2 -exec /bin/rm -rf '{}' \; &&
/usr/bin/find /tmp -type l -ctime +2 |xargs /bin/rm -f &&
/usr/bin/find -L /tmp -mtime +2 -print -exec rm -f {} \;
Just save the contents above to a file chmod 775 the file and create a cron entry to run it. Since this is a web server we don't want to reboot it for obvious reasons.
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:
TMPTIME
says how frequent the tmp dir sould be cleared in daysWhile 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 FrequencyThe 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 isTMPTIME
.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
.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.I'm checking this on Ubuntu 16.10. I can certify that editing /etc/default/rcS has no effect at all anymore and the files in tmp are wiped out by reboot no matter what you put in that file. As others mention, tmpreaper is no longer used.
I think the right answer is that Ubuntu 16.10 has a new setup. There is a folder /etc/tmpfiles.d, documented in the man page "tmpfiles.d". In that folder, one should place a configuration file to control whether the /tmp is to be erased. This is what I am doing to stop reboots from erasing files in /tmp unless they are 20 days old:
Replace "20d" by "-" if you never want files deleted. This is my best effort, that man page is nearly impenetrable with detail.
The advantage of the new setup is that a file cleaner can still run even if the system is not rebooted (as in the case of an always on server). That's a big plus, I think.
In Ubuntu 14.04 this is done by
tmpreaper
, which is called daily by cron (from/etc/cron.daily
). The program can be configured via/etc/default/rcS
and/etc/tmpreaper.conf
.In a
systemd
Ubuntu (15.10 and newer), this is done by systemd, using thesystemd-tmpfiles-clean
service and timer:And
You can change the timer behaviour itself using
systemctl edit systemd-tmpfiles-clean.timer
, and using various systemdTimer
configuration options (seeman 5 systemd.timer
).So
systemd-tmpfiles-clean
runs on shutdown, and once per day otherwise. The files it cleans can be extended using/etc/tmpfiles.d
(mentioned in another answer).It's interesting to note that as default
/etc/tmpfiles.d
is empty. The file where the/tmp
policy is defined is here:Actual content:
So, as already noted, to override it, you can see the other answer about tmpfiles.d.
Before 14.04:
It is cleaned up every time you reboot.
On one of our servers running Ubuntu, we have a script to remove files in /tmp and it runs nightly.
The script is:
Just save the contents above to a file chmod 775 the file and create a cron entry to run it. Since this is a web server we don't want to reboot it for obvious reasons.