Skip to content

Temp Files

systemd-tmpfiles provides a structured and configurable method to manage temporary directories and files. When systemd starts a system, one of the first service units launched is systemd-tmpfiles-setup.

This service runs the command systemd-tmpfiles --create --remove . This command reads configuration files from /usr/lib/tmpfiles.d/*.conf, /run/tmpfiles.d/*.conf, and /etc/tmpfiles.d/*.conf.

To ensure systems do not fill up their disks with stale data, a systemd timer unit called systemd-tmpfiles-clean.timer triggers systemd-tmpfiles-clean.service on a regular interval, executing systemd-tmpfiles --clean.

Use the systemd-tmpfiles --clean purge all files which have not been accessed, changed, or modified more recently than the maximum age defined.

The files under /etc/tmpfiles.d/ are meant to configure custom temporary locations.

Example:

cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d/tmp.conf
vi /etc/tmpfiles.d/tmp.conf
q /tmp 1777 root root 30d
d /mytemp 0770 root root 1d

systemd-tmpfiles --create

Summary

The systemd timer units can execute the recurring jobs.

Command References:

systemd-tmpfiles, tmpfiles.d and stat.