How to auto clean your /tmp directory using tmpwatch


You may find that /tmp becomes full regularly.  This is usually the result of user’s PHP scripts or Mysqld scripts that aren’t removing files they create in /tmp.
There is a third-party program for clearing the /tmp directory called tmpwatch.

in this article, I’ll show to you how to install tmpwatch and set up a cron job so that it can auto clean the /tmp directory of old files on a regular basis.


1. Install tmpwatch if it’s not installed. If you run this command when tmpwatch is already installed and on the latest version, it will tell you there is nothing to do and no harm will be caused.
go to your Terminal and login to SSH using WHM Terminal or regular terminal you have on your PC, and run this command.

yum install tmpwatch

2. Run tmpwatch using this command to clear the /tmp partition of files over a week old now, a week contains 168 hours so change 168 to any value you want, but make sure to not use less than 12 hours!

tmpwatch --mtime --all 168 /tmp

3. Add tmpwatch to root crontab to clear old files (more than a week old) every night.  If you needed to lower the number of hours in step 2, lower it here as well.

echo "0 0 * * * tmpwatch -mtime --all 168 /tmp" >> /var/spool/cron/root

Directory for personal crontab files -change /var/spool/cron/root as the system you use-:

  1. Mac OS X – /usr/lib/cron/tabs/ for users (/usr/lib/cron/tabs/username).
  2. FreeBSD & OpenBSD & NetBSD – /usr/lib/cron/tabs/ for users (/usr/lib/cron/tabs/username).
  3. Centos & Red Hat / RHEL & Fedora – /var/spool/cron/ for users (/var/spool/cron/username)
  4. Debian & Ubuntu – /var/spool/cron/crontabs/ for users (/var/spool/cron/crontabs/username).

Useful Commands:
To create and edit cronjobs: crontab -e
To view the crontab files: crontab -l

after running the previous command you need to run one last command to Reload crontab.
4. Reload the crontab so that the new job takes action.

/scripts/restartsrv_crond --reload

Hope it’s easy for you!
comment if you need any assistant.


Share this article:

One Reply to “How to auto clean your /tmp directory using tmpwatch”

Leave a Reply

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