Clean tmp folder in Linux

On Linux servers, you may face disk space or inode limit reaching on /tmp partition due to large number of temporary files. Most of the time it will be PHP sesion files. To clean these files, you can use a cronjob like the following. It have some common extension i found on shared cpanel server, files in your sever may be differnt. You need to investigate what is taking up disk space on your /tmp folder and modify the script as needed.

Create file

vi /usr/local/bin/cleantmp

Add following

#!/bin/bash

find /tmp -type f -name 'sess_*' -mtime +30 | xargs rm -f
find /tmp -type f -name 'php*' -mtime +30 |xargs rm -f
find /tmp -type f -name '*.zip' -mtime +30 |xargs rm -f
find /tmp -type f -name 'tmp_*' -mtime +30 |xargs rm -f
find /tmp -type f -name '*.jpg' -mtime +30 |xargs rm -f
find /tmp -type f -name '*.tmpvideo' -mtime +30 |xargs rm -f
find /tmp -type f -name '*.params' -mtime +30 |xargs rm -f
find /tmp -type f -name '*.gif' -mtime +30 |xargs rm -f
find /tmp -type f -name '*.png' -mtime +30 |xargs rm -f

-mtime +30 is to find files that are modified 30 or more days ago.

Make it executable

chmod 755 /usr/local/bin/cleantmp

Create a cronjon

crontab -e

Add

0 1 * * *  /usr/local/bin/cleantmp > /dev/null 2>&1

See tmp

Comments

Leave a Reply

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