210

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

1

8 Answers 8

211

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 Ubuntu 14: using tmpreaper which gets called by /etc/cron.daily, configured via /etc/default/rcS and /etc/tmpreaper.conf. (Credits to this answer).
  • On Ubuntu 16: using tmpfiles.d. (Credits to this answer).
  • On other Debian-like systems: on boot (the rules are defined in /etc/default/rcS).
  • On RedHat-like systems: by age (RHEL6 it was /etc/cron.daily/tmpwatch ; RHEL7/RHEL8 and RedHat-like with systemd it's configured in /usr/lib/tmpfiles.d/tmp.conf, called by systemd-tmpfiles-clean.service).
  • On Gentoo /etc/conf.d/bootmisc.
7
  • 25
    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, 2012 at 15:14
  • There are also packages available that can remove them periodically. Apr 6, 2012 at 20:15
  • 3
    On RedHat-like systems with systemd (centos7/rhel7), it's configured in /usr/lib/tmpfiles.d/tmp.conf. It's called by systemd's target systemd-tmpfiles-clean.service. Mar 13, 2015 at 12:06
  • 1
    On legacy Debian, you can consider tmpreaper package, it's forked version of tmpwatch.
    – Věroš K.
    Oct 12, 2017 at 9:37
  • 2
    Ubuntu 19.04: cat: /etc/default/rcS: No such file or directory Oct 10, 2019 at 22:17
28

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.

5
  • I don't have this on CentOS 7.4. Feb 15, 2018 at 11:06
  • @KevinLemaire The functionality was moved into a systemd service. lists.centos.org/pipermail/centos/2014-October/147155.html
    – cjc
    Feb 15, 2018 at 15:30
  • If you didn't find the tmpwatch file, you can download tmpwatch by using yum install tmpwatch Nov 12, 2018 at 8:18
  • Can I get tmpwatch on other distros? Sounds amazing. I'm on debian based distro
    – Jonathan
    May 14, 2019 at 22:40
  • sudo find /tmp -type f -atime +10 -delete will delete tmp files that haven't been accessed in 10 days, use with care
    – Jonathan
    May 14, 2019 at 22:42
16

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

8

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.

6

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.

5

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

Sorta-tempy files that you do not want cleared on boot go in /var/tmp

That's what it's for :-)

4

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)

  1. Edit the /etc/sysconfig/cron file manually in command line.
  2. Open Yast and navigate into System -> /etc/sysconfig Editor -> System -> Cron
0

... files in /tmp, and I wondered about the rules on deleting them?

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

The /tmp directory must be made available for programs that require temporary files.

Programs must not assume that any files or directories in /tmp are preserved between invocations of the program.

/var/tmp

The /var/tmp directory is made available for programs that require temporary files or directories that are preserved between system reboots. Therefore, data stored in /var/tmp is more persistent than data in /tmp.

Files and directories located in /var/tmp must not be deleted when the system is booted. Although data stored in /var/tmp is typically deleted in a site-specific manner, it is recommended that deletions occur at a less frequent interval than /tmp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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