Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How is the /tmp directory cleaned up? Is it automatic? If so, how frequently is it cleaned up?

share|improve this question
4  
My temporary files never get written to the disk. They get written to a RAM disk. I did put tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 in /etc/fstab. –  Anonymous Nov 16 '11 at 23:43

5 Answers 5

up vote 73 down vote accepted

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

share|improve this answer
2  
Clearing at every boot is not ideal for a machine that is never rebooted, like a server. I've got over 500,000 files taking 5Gb space in my /tmp because my server has 378 days uptime. I'm reluctant to reboot it, simply because clearing those files on reboot make take hours. –  rjmunro Jan 25 '12 at 11:47
2  
In your case you should give tmpreaper a chance. –  qbi Feb 24 '13 at 23:01
    
A CRON job could easily solve that. –  Ken Sharp Mar 8 at 14:27

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.

share|improve this answer
1  
good explanation. But in which script is the cleanup command? I have seen /etc/init/mounted-temp.conf, but it has the line start on mounted MOUNTPOINT=/tmp that make me think it is non applicable. –  enzotib Jan 9 '11 at 19:56
    
If you don't want a file to be removed automatically, put it in /var/tmp instead of /tmp. –  Gilles Jan 10 '11 at 21:29
    
It's also handy to keep files you don't want to lose (rendered video frames) when your machine crashes, possibly due to OOM. A better solution would be to fix the problem, of course. :-) –  Ken Sharp Mar 8 at 14:28
    
Can this be done inside Cygwin? –  CMCDragonkai May 14 at 8:19

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. That basically means at every boot.

The script does roughly the following: if a file in /tmp is older than $TMPTIME days it will be deleted.

$TMPTIME is an environment variable defined in /etc/default/rcS. The default value of $TMPTIME is 0, which means every file and directory in /tmp gets deleted.

share|improve this answer
3  
This is the most precise and to the point answer. It should be accepted by the OP. –  Maxim Veksler Aug 7 '12 at 11:21
1  
@MaximVeksler that's not your call. The OP can decide which answer they want to select. And given the age of this thread, its likely that will not change, there is a lockout unless answers get edited. –  Thomas W. Aug 7 '12 at 17:49
    
Perfect answer! –  Nitin Bansal Jun 11 at 10:49

It is cleaned up every time you reboot.

share|improve this answer

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.

share|improve this answer
1  
You might be better off using tmpwatch. –  poolie Sep 28 '11 at 4:59

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.