How to clean /tmp directory automatically in Linux/cPanel using tmpwatch

The “tmpwatch ” command in linux is to removes files which haven’t been accessed for a period of time. The tmpwatch recursively removes files which haven’t been accessed for a given time. Normally, it’s used to clean up directories which are used for temporary holding space such as /tmp.

If you are noticing “/tmp” getting overloaded with files and not sure which files/folders to delete – you better use “tmpwatch” to cleanup or delete files/folders from the “/tmp” directory.

You’ll need SSH root access to install tmpwatch and add it in the cron. If your server is inaccessible due to “/tmp” getting full – you may restart the server and that should free up some space after reboot.

  1. Login to the server as root using SSH
  2. Run the following command:

    #yum install tmpwatch -y

  3. To delete temporary files (for example after every 12 hours) run the following command:

    #/usr/sbin/tmpwatch -am 12 /tmp

  4. The next step is to configure tmpwatch to run automatically through a cron. To do that type the following command:

    #crontab -e

  5. The above command will open the cron job list for the user root. Now go to the bottom and add the following line and save the file:

    0 4 * * * /usr/sbin/tmpwatch -am 12 /tmp

    If you are unable to add the above line, you may navigate to “/var/spool/cron” and open the cron file “root” with a text editor (such as, vi, nano). Add the line at the bottom and save the file:

    0 4 * * * /usr/sbin/tmpwatch -am 12 /tmp

Check the usage of “/tmp” and it should be clean by now.

Thank you.

Leave a Reply

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