#!/bin/bash
# cleantmp.sh - Remove unused files from tmp directories

# ------------- Default Configuration --------------------
# TMP_DIRS - List of directories to search
# FILE_AGE - # days ago (rounded up) that file was last accessed
# LINK_AGE - # days ago (rounded up) that symlink was last accessed
# SOCK_AGE - # days ago (rounded up) that socket was last accessed

TMP_DIRS="/tmp /var/tmp /usr/src/tmp /mnt/tmp"
FILE_AGE=+1
LINK_AGE=+1
SOCK_AGE=+1

#-----------------------------------------------------------------
cd /
/usr/bin/logger "cleantmp.sh[$$] - Begin cleaning tmp directories"

# delete any tmp files that are more than 2 days old
/usr/bin/find $TMP_DIRS                                         \
     -depth                                                     \
     -type f -a -ctime $FILE_AGE                                \
     -print -delete

# delete any old tmp symlinks
/usr/bin/find $TMP_DIRS                                         \
     -depth                                                     \
     -type l -a -ctime $LINK_AGE                                \
     -print -delete

# delete any empty files
/usr/bin/find $TMP_DIRS                                         \
     -depth                                                     \
     -type f -a -empty                                          \
     -print -delete

# Delete any old Unix sockets
/usr/bin/find $TMP_DIRS                                         \
     -depth                                                     \
     -type s -a -ctime $SOCK_AGE -a -size 0                     \
     -print -delete

# delete any empty directories (other than lost+found)
/usr/bin/find $TMP_DIRS                                         \
     -depth -mindepth 1                                         \
     -type d -a -empty -a ! -name 'lost+found'                  \
     -print -delete

/usr/bin/logger "cleantmp.sh[$$] - Done cleaning tmp directories"
exit 0
