Using tmpwatch to clear files from /tmp partition
Introduction
The tmpwatch utility helps automatically remove old and unused files from the /tmp directory. Since applications such as Apache and MySQL store temporary files in this location, the partition can fill up over time, potentially causing service interruptions. Using tmpwatch helps keep the /tmp directory clean and prevents disk space issues.
Prerequisites
- Root or sudo access to the Linux server
- SSH access to the server
- yum Package manager (for CentOS systems)
Implementation
Step 1
Install the tmpwatch utility.
yum -y install tmpwatch
Step 2
Run the following command to remove files from the /tmp directory that are older than 24 hours.
tmpwatch –all –mtime 24 /tmp
Step 3
To automate the cleanup, add the following entry to the system crontab:
@daily /usr/sbin/tmpwatch –all –mtime 24 /tmp
Step 4
Save the crontab and verify that the scheduled task is added successfully.
Conclusion
Using tmpwatch to clean the /tmp directory helps prevent temporary files from consuming disk space and reduces the risk of service disruptions caused by a full /tmp partition. Scheduling the cleanup as a daily cron job ensures that old temporary files are removed automatically.
