Clearing /tmp and /var/tmp with find in cron.daily

Moving from Solaris to Linux has been enriching, but not without subtle differences. One of many was the need for older SLES servers to clear out /tmp and /var/tmp. I started with the standard Solaris cron jobs:

/usr/bin/find /tmp/* -mount -depth -mtime +30 -a -exec rm -rf {} \; > /dev/null 2>&1
/usr/bin/find /var/tmp/* -mount -depth -mtime +30 -a -exec rm -rf {} \; > /dev/null 2>&1

In spite of how active some of these servers are, if /tmp or /var/tmp are empty, the above commands return a value of 1 instead of 0, thus resulting in a nice email that cron failed. Removing the asterisk was an easy way to fix this, however that posed the problem of deleting /tmp or /var/tmp itself if there was no activity on the server for 30 days. Unlikely, but possible. Rather than touching a file before the find commands (thus moving mtime to current), I opted to exclude directories and only remove files (adding the -type f option). Even though directories can still exist, they take up very little space and will likely get cleaned up on a reboot at some point. This was a happy medium. My script now looks like this:

/usr/bin/find /tmp/ -type f -mount -depth -mtime +30 -a -exec rm -rf {} \; > /dev/null 2>&1
/usr/bin/find /var/tmp/ -type f -mount -depth -mtime +30 -a -exec rm -rf {} \; > /dev/null 2>&1

Leave a Reply

Your email address will not be published. Required fields are marked *