Using tmpwatch to clear files from the /tmp partition

Introduction

In a Linux system, services such as Apache and MySQL may create temporary files in the /tmp partition. If the /tmp partition becomes full, it can affect system performance and may cause services to stop or crash.

To prevent this, the tmpwatch utility can be used to automatically remove old files from /tmp.

Prerequisites

  • A CentOS/RHEL-based Linux server
  • Root or sudo access
  • yum package manager
  • Basic knowledge of Linux commands and cron

Implementation

Step 1: Install tmpwatch.

On CentOS, install the tmpwatch utility using yum:

yum -y install tmpwatch

Step 2: Clear Old Files from /tmp

Execute the following command to clear files from the /tmp partition:

tmpwatch –all -mtime 24 /tmp

The above command deletes files that are older than 24 hours.

Step 3: Schedule tmpwatch Using Crontab

To perform this cleanup periodically, add the command to the crontab:

@daily * * * *  /usr/sbin/tmpwatch –all –mtime 24

This allows the cleanup task to run automatically on a scheduled basis instead of requiring manual execution.

Conclusion

The tmpwatch utility provides a simple way to remove old temporary files from the /tmp partition. Scheduling the cleanup through cron can help prevent /tmp from becoming full and reduce the risk of services being affected by insufficient temporary storage.

FAQs

1. What is tmpwatch?

tmpwatch is a Linux utility used to remove files that have not been accessed for a specified period of time.

2. Why should I clean the /tmp partition?

A full /tmp partition can cause applications and system services to experience errors or fail because they cannot create required temporary files.

3. How does -mtime 24 work?

The -mtime 24 option specifies the age threshold used by the command, allowing old files to be removed based on the configured time period.

4. Can tmpwatch be scheduled automatically?

Yes. You can add the tmpwatch command to the system’s crontab so that the cleanup runs automatically.

Downgrade PHP 5.3 to 5.2 in CentOS 6 – Learn how to manage PHP versions on CentOS and configure an older PHP version for legacy application compatibility. Read the article

How to Configure VNC (Virtual Network Computing) on CentOS – A practical guide to installing and configuring VNC for remote graphical access to a CentOS server. Read the article

Talk to our experts

Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team here.

admin

Writes about Web & Architecture at Pheonix Solutions.

Leave a Reply

Scroll to Top