HOWTO clean /tmp
From Gentoo Linux Wiki
| Installation • Kernel & Hardware • Networks • Portage • Software • System • X Server • Gaming • Non-x86 • Emulators • Misc |
Contents |
[edit] Introduction
Sometimes programs don't clean temporary files in /tmp. Cleaning it manually is a hard task, but there are some utilities to do that for you.
[edit] Baselayout
Add this line, to have /tmp wiped during bootup:
| File: /etc/conf.d/bootmisc |
|
WIPE_TMP="yes"
|
[edit] tmpwatch
Tmpwatch is a simple program which removes files which haven't been accessed for a period of time.
# emerge tmpwatch
Uncomment all examples from /etc/cron.daily/tmpwatch. The file should look like this:
| File: /etc/cron.daily/tmpwatch |
TMPWATCH="/usr/sbin/tmpwatch"
PORTAGE_TMPDIR="$(portageq envvar PORTAGE_TMPDIR)/portage"
DISTDIR="$(portageq distdir)"
if [[ -d /tmp ]]; then
${TMPWATCH} --atime 168 /tmp
fi
if [[ -d ${PORTAGE_TMPDIR:-/var/tmp/portage} ]]; then
${TMPWATCH} --mtime --all 336 ${PORTAGE_TMPDIR:-/var/tmp/portage}
fi
if [[ -d ${DISTDIR:-/usr/portage/distfiles} ]]; then
${TMPWATCH} --atime --fuser 4320 ${DISTDIR:-/usr/portage/distfiles}
fi
|
The script is run everyday by cron and performs the following tasks:
- Deletes everything in /tmp that haven't been accessed in a week.
- Deletes everything in PORTAGE_TMPDIR that hasn't been modified in 2 weeks.
- Deletes everything in DISTDIR that hasn't been accessed in 6 months.
Warning: On filesystems mounted with the noatime code> flag, access times are usually equal to the creation time of the file. Make sure you add --ctime code> flags if this is the case with your tmp directories.
[edit] tmpreaper
This Debian utility cleans temporary files depending on atime, mtime or ctime (you can configure that). First emerge it: emerge tmpreaper.
[edit] Cron job
The easiest method is to use bundled script:
| Code: |
# bzcat /usr/share/doc/tmpreaper-1.6.5/cron.daily.bz2 > /etc/cron.daily/tmpreaper.cron # chmod +x /etc/cron.daily/tmpreaper.cron |
[edit] Configuration (optional)
tmpreaper's configuration file is /etc/tmpreaper.conf. In this file you setup env variables, which then are sourced by the cron script. If you don't set some variable, tmpreaper will use default value. You don't even have to create /etc/tmpreaper.conf at all, as defaults are suitable for most systems.
- TMPREAPER_TIME - max. age of files before they're removed.
- TMPREAPER_PROTECT_EXTRA - extra patterns (like in bash) that you may want to protect
- TMPREAPER_DIRS - list of directories to clean up, separated by space
- TMPREAPER_ADDITIONALOPTIONS - additional options passed to tmpreaper
[edit] Alternative: Using temporary disk space
Compile your kernel with tmpfs (temporary file system) enabled and mount your /tmp directory using it. The useful bit here is that nothing will be written to your hard drive on this mount point as it will act like a RAM disk (however nothing will be saved either). The advantage of tmpfs over the more traditional ramfs is that it lives in the kernel internal cache and grows and shrinks to accommodate the files placed there.
If you don't specify a maximum size, it will default to a ceiling limit of half your available memory. An example /etc/fstab with 100MB temporary ram file mounted on /tmp would look like:
tmpfs /tmp tmpfs size=100m,mode=1777 0 0
The use of the /tmp cleaning tools above is still encouraged since they'll clean up tmpfs memory usage during a long uptime, as well as make the space reusable after it has been filled - this can be critical on servers. Using atime (last file access time) for cleanup is also a good idea since atime updates don't have any overhead with tmpfs.
For more information read: /usr/src/linux/Documentation/filesystems/tmpfs.txt
