In this article, you will learn how to configure a timer for managing temporary files. In most modern Linux systems, a large number of temporary files and directories are required for optimal processing. If not cleaned often, they may accumulate GB of storage space. Therefore, it is necessary to clear old files so that they do not take up disk space.
Some users / applications will use / tmp Directories for temporary data, while other directories use task-specific locations, such as daemons and user-specific volatile directories, /run . Volatility means that the file exists only in memory. If the system restarts or loses power, everything in volatile storage will disappear.
Automatically clear unused temporary files in Linux
In Red Hat Enterprise Linux 7 and later, one called systemd-tmpfiles Already included. The tool provides a structured and configurable way to manage temporary directories and files.
You can check the services started with:
$ 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: Starting Create Volatile Files and Directories... Feb 10 08:27:50 envoy-nginx.novalocal systemd: 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: Starting Create Static Device Nodes in /dev... Feb 10 08:27:49 envoy-nginx.novalocal systemd: Started Create Static Device Nodes in /dev.
When starting the systemd-tmpfiles-setup service unit, it will run systemd-tmpfiles-create-delete command. This command checks the configuration file from:
If there are files and directories marked for deletion in the above configuration files, they will be deleted. For files and directories marked for creation, create them with the correct permissions if necessary.
How to clear temporary files with Systemd timer
A system timer unit named systemd-tmpfiles-clean.timer will trigger systemd-tmpfiles-clean.service at regular intervals, and then execute the systemd-tmpfiles -clean command.
You will specify how often to start the service in Start [Timer] section. See the 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 the above example, systemd-tmpfiles-clean.service will be triggered 15 minutes After the system starts. Any other triggers occur 24 hours after the last service trigger. You can adjust the value to your liking.
If you make changes, make sure to reload the service.
sudo systemctl daemon-reload sudo systemctl enable --now systemd-tmpfiles-clean.timer
How to manually clean up temporary files
Let’s configure systemd-tmpfiles to clean up the / mytmp directory. This will ensure that the directory does not contain the last unused files 3 days .
You can copy the sample configuration file and update it-/usr/lib/tmpfiles.d/tmp.conf
Edit the file as follows.
$ 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 the directness of having the correct ownership, create a configuration like the following.
d /run/mytmp 0700 root root 60s
The last unused file in the directory 60 The seconds must be cleared.
After creating the file, use the following command to ensure that the file contains the appropriate configuration.
sudo systemd-tmpfiles --create /etc/tmpfiles.d/mytmp.conf
If you don’t see any errors in the output, your configuration settings are correct. You can call manual cleanup at any time using the following command:
systemd-tmpfiles --clean /etc/tmpfiles.d/mytmp.conf
For more details, see the following man pages.
- tmpfiles.d (5)
- systemd-tmpfiles (8),
- systemd.timer (5)
The best Linux books for beginners and experts
Popular books for preparing for CRISC certification exams
Top Certified Information Security Manager (CISM) Study Books