How to automatically clean up /tmp directory contents in Linux?

How to automatically clean up /tmp directory contents in Linux?

 

Every Linux system has a directory called /tmp which has mounted with separate file system.

It has special filesystem called tmpfs. It’s a virtual filesystem and operating system will automatically mount /tmp mount point while system booting.

If you want to mount /tmp directory separately as per the application requirement.

Yes, you can mount it and it should be added to the /etc/fstab file.

/tmp directory is a directory used to hold temporary files (or session files) when application is running.

Those temporary files will be deleted automatically by application once their process is got completed.

By default /tmp directory is cleaned up only at the time of system startup or reboot.

By default applications are automatically delete their contents from this directory once their process is got completed. But some of the applications won’t do.

Hence, we need to remove those files manually but if we delete some of the live files from this directory which leads to disconnect the current session that has established.

However, if /tmp directory got filled up and we need to remove unused files or old session files or dead files to free up some disk space on it.

Otherwise the applications which are running on the server doesn’t work and you will be getting some error message when they are trying to write session files on /tmp directory.

In this scenario, what would be the best approach to remove /tmp directory contents.

Use the df command to check the /tmp directory has mounted as separately. Yes as per the below output the /tmp has mounted separately.

# df -h
Filesystem      Size  Used Avail Use% Mounted on
dev             7.8G     0  7.8G   0% /dev
run             7.8G  1.7M  7.8G   1% /run
/dev/nvme0n1p1  217G  129G   78G  63% /
tmpfs           7.8G  841M  7.0G  11% /dev/shm
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
tmpfs           7.8G  106M  7.7G   2% /tmp
/dev/loop0      109M  109M     0 100% /var/lib/snapd/snap/odrive-unofficial/2
/dev/loop1       91M   91M     0 100% /var/lib/snapd/snap/core/6405
/dev/loop2       90M   90M     0 100% /var/lib/snapd/snap/core/6130
tmpfs           1.6G   12K  1.6G   1% /run/user/120
tmpfs           1.6G   52K  1.6G   1% /run/user/1000
/dev/sda2       932G  622G  311G  67% /run/media/daygeek/DATA

You can navigate to “/tmp” mount point to see what kind of files were occupied by the /tmp.

# ls -lh /tmp
total 0
drwxr-xr-x 3 daygeek daygeek 80 Apr 28 22:20 checkup-db-daygeek
-rw-r----- 1 daygeek daygeek  0 Apr 28 22:20 qipc_sharedmemory_MSMNotifier1982c3c75cbed4786bc185973fce6242a7b208b8
-rw-r----- 1 daygeek daygeek  0 Apr 28 22:20 qipc_systemsem_MSMNotifier1982c3c75cbed4786bc185973fce6242a7b208b8
srwxr-xr-x 1 daygeek daygeek  0 Apr 29 08:43 qtsingleapp-notepa-8945-3e8
drwx------ 3 root    root    60 Apr 28 22:19 systemd-private-7c36847f844143cba83f3a0fdd8623b7-colord.service-0SAr1l
drwx------ 3 root    root    60 Apr 28 22:19 systemd-private-7c36847f844143cba83f3a0fdd8623b7-ModemManager.service-4jLbNd
drwx------ 3 root    root    60 Apr 28 22:19 systemd-private-7c36847f844143cba83f3a0fdd8623b7-systemd-timesyncd.service-iuoel6
drwx------ 3 root    root    60 Apr 28 22:19 systemd-private-7c36847f844143cba83f3a0fdd8623b7-upower.service-ApTFNf
drwx------ 3 daygeek daygeek 60 Apr 29 08:35 Temp-7767ec79-0c9d-405e-a456-52718e66292d
drwx------ 2 daygeek daygeek 40 Apr 28 22:22 Temp-9bb8163c-aec2-47ce-a0b1-597ab299807a
drwx------ 2 daygeek daygeek 40 Apr 29 11:14 tracker-extract-files.1000

It can be achieved by using the below three options.

  • atime: File Last Access Time – The last time the file (or directory) data was accessed
  • ctime: The last time the inode status was changed.
  • mtime: The last time the file (or directory) data was modified.

Method-1 : How To Delete /tmp Files Older Than “X” Days In Linux Using mtime

These commands will help you to remove files older than “X” Days. It’s up to you, how do you want to perform this. You can use the options based on your requirements.

To delete /tmp files oldern than 2 days on /tmp directory using mtime, run the following command.

# find /tmp -type f -mtime +2 -delete;
or
# find /tmp -type f -mtime +2 -exec rm -f {} \;
or
# find /tmp -type f -mtime +2 | xargs rm -f

Method-2 : How To Delete /tmp Files Older Than “X” Days In Linux Using atime

These commands will help you to remove files older than “X” Days. It’s up to you, how do you want to perform this. You can use the options based on your requirements.

To delete /tmp files oldern than 2 days on /tmp directory using atime, run the following command.

# find /tmp -type f -atime +2 -delete;
or
# find /tmp -type f -atime +2 -exec rm -f {} \;
or
# find /tmp -type f -atime +2 | xargs rm -f

Method-3 : How To Delete /tmp Files Older Than “X” Hours In Linux Using ctime

To delete /tmp files oldern than 5 hours on /tmp directory using ctime, run the following command.

# find /tmp -type f -mmin -300 -delete;
or
# find /tmp -type f -mmin -300 -exec rm -f {} \;
or
# find /tmp -type f -mmin -300 | xargs rm -f

Method-4 : How To Delete /tmp Files Older Than “X” Hours In Linux Using Shell Script

The above methods are requires human interaction to perform the task.

However, we can’t keep eye on this by 24/7. If you have 1000+ servers, what will be the solution?

It should be automated via script. To clean up the /tmp directory, we can write a small shell script.

The script will delete /tmp files older than 5 hours.

# vi /opt/script/tmp-cleanup.sh

#!/bin/bash
find /tmp -type f -mmin -300 -exec rm -f {} \;

Finally add a cronjob to automate this. It will run every five hours.

# crontab -e
0 */5 * * * /bin/bash /opt/script/tmp-cleanup.sh

Similar Posts

Leave a Reply