Using a cronjob to clean /tmp

/tmp is cleaned on every system reboot, but for systems that cannot be rebooted often, and specially if the only objective is to clean /tmp a script can be made to clean it.

No process should keep important data there but it is important that you are able to clean /tmp without disrupting any processes writing data in tmp.

I recommend to use tmpreaper for that.

You can install it with sudo apt-get install tmpreaper.

From the man page:

tmpreaper recursively searches for and removes files and empty directories which haven't been accessed for a given number of seconds. Normally, it's used to clean up directories which are used for temporary holding space, such as "/tmp". Please read the WARNINGS section of this manual.

Usage normally involves invoking tmpreaper in /tmp with a time spec argument. The time spec argument can be anything from d for days, h for hours, m for minutes, or s for seconds.

Please use the --test to dry run and get an output of the results before you actually run the command. That will get you an idea of what will be deleted before you actually commit any changes to the file system.


Try adding a cronjob which executes

find /tmp/* -type d -mtime +5 -print0 | xargs -0 rm -rf 
find /tmp/* -type f -mtime +5 -print0 | xargs -0 rm -rf 

This would delete all files older than 5 days.

Tags:

Cron

Tmp