Saturday, 29 August 2020

Automatically Clean Unused Temporary files in Linux

Linux Tutorial and Material, Linux Certification, Linux Exam Prep, LPI Exam Prep

How can I remove/clean unused files in Linux?. In this article, you will learn how to configure a timer that manages temporary files. In most Modern Linux systems, a large number of temporary files and directories are required for optimal processing. Accumulatively, they could consume gigabytes of storage space if not cleaned frequently. It is therefore necessary to purge the old files so that they do not fill up disk space.

Some users/applications will use the /tmp directory to hold temporary data, while others use a more task-specific location such as daemon and user-specific volatile directories under /run. Volatile means that the files only exist in memory. If the system is rebooted or when there is a lose of power, all the contents of volatile storage will be gone.

Automatically Clean Unused Temporary files in Linux


In Red Hat Enterprise Linux 7 and later, a new tool called systemd-tmpfiles is included. This tool provides a structured and configurable method to manage temporary directories and files.

You can check the services started with the command:

$ systemctl status  systemd-tmpfiles-*
● systemd-tmpfiles-setup.service - Create Volatile Files and Directories
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-setup.service; static; vendor preset: disabled)
   Active: active (exited) since Mon 2020-02-10 08:27:50 EAT; 1 weeks 3 days ago
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)
  Process: 794 ExecStart=/usr/bin/systemd-tmpfiles --create --remove --boot --exclude-prefix=/dev (code=exited, status=0/SUCCESS)
 Main PID: 794 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/systemd-tmpfiles-setup.service

Feb 10 08:27:50 envoy-nginx.novalocal systemd[1]: Starting Create Volatile Files and Directories...
Feb 10 08:27:50 envoy-nginx.novalocal systemd[1]: Started Create Volatile Files and Directories.

● systemd-tmpfiles-setup-dev.service - Create Static Device Nodes in /dev
   Loaded: loaded (/usr/lib/systemd/system/systemd-tmpfiles-setup-dev.service; static; vendor preset: disabled)
   Active: active (exited) since Mon 2020-02-10 08:27:49 EAT; 1 weeks 3 days ago
     Docs: man:tmpfiles.d(5)
           man:systemd-tmpfiles(8)
  Process: 553 ExecStart=/usr/bin/systemd-tmpfiles --prefix=/dev --create --boot (code=exited, status=0/SUCCESS)
 Main PID: 553 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/systemd-tmpfiles-setup-dev.service

Feb 10 08:27:49 envoy-nginx.novalocal systemd[1]: Starting Create Static Device Nodes in /dev...
Feb 10 08:27:49 envoy-nginx.novalocal systemd[1]: Started Create Static Device Nodes in /dev.

When the systemd-tmpfiles-setup service unit is started, it runs the systemd-tmpfiles –create –remove command. The command checks for configuration files from:

◉ /usr/lib/tmpfiles.d/.conf
◉ /run/tmpfiles.d/.conf
◉ /etc/tmpfiles.d/*.conf

If there are files and directories marked for deletion in above configuration files, the’ll be removed. For the files and directories marked for creation, they are created with the correct permissions if necessary.

How Temporary Files are Cleaned with a Systemd Timer


A systemd timer unit called systemd-tmpfiles-clean.timer triggers the systemd-tmpfiles-clean.service on a regular interval, which then executes the systemd-tmpfiles –clean command.

You’ll specify how often the service should be started in the [Timer] section. See example below.

$ cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d

In above example, the systemd-tmpfiles-clean.service will be triggered 15 minutes after the system has booted up. Any other trigger happens 24 hours after the last service trigger. You can adjust the values to your liking.

If you make a change, ensure you reload service.

sudo systemctl daemon-reload
sudo systemctl enable --now systemd-tmpfiles-clean.timer

How To Manually Clean Temporary Files


Let’s configure systemd-tmpfiles to clean the /mytmp directory. This will ensure the directory does not contain files that that have not been used in the last 3 days.

You can copy the example configuration file and update it – /usr/lib/tmpfiles.d/tmp.conf

Edit the file like below.

$ sudo vim /etc/tmpfiles.d/mytmp.conf
See tmpfiles.d(5) for details
# Clear tmp directories separately, to make them easier to override
q /mytmp 1777 root root 3d

If you want to ensure a directoty exist with correct owneship, create a configuration like below.

d /run/mytmp 0700 root root 60s

Any file in this directory that remains unused in the last 60 seconds must be purged.

Once the file is created, use the following command to ensure the file contains the appropriate configuration.

sudo systemd-tmpfiles --create /etc/tmpfiles.d/mytmp.conf

If you don’t see any error in the output, then the it confirms that the configuration settings are correct. You can invoke a manual clean anytime with the command:

systemd-tmpfiles --clean /etc/tmpfiles.d/mytmp.conf

Related Posts

0 comments:

Post a Comment