WittCode💻

Is Your tmp Directory Working?

By

Learn how the Linux tmp directory works and how to use it. We will go over the systemd-tmpfiles configuration syntax, journalctl logging of clean up events, and more.

Table of Contents 📖

The tmp Directory

The tmp directory is used to store temporary files created by both system processes and applications in Linux-based systems. Examples of temporary files saved to tmp are cached data, stored session information, or intermediate processing data.

INFO: The tmp directory itself isn't anything inherently special, it's just another directory in the filesystem. What makes it distinct are the conventions and practices associated with its usage.

To check if and when your system is scheduled to clean up the tmp directory, use the systemctl list-timers command. This will list all active timers on your system and show their status, including any related to cleanup tasks. The entries that we are interested in are those containing systemd-tmpfiles-clean.timer in the output.

systemctl list-timers
NEXT                         LEFT    LAST                         PASSED       UNIT                         ACTIVATES
Thu 2025-01-02 05:58:54 UTC  6h left Wed 2025-01-01 05:58:54 UTC  17h ago      systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Thu 2025-01-02 06:51:31 UTC  7h left Wed 2025-01-01 06:19:24 UTC  17h ago      apt-daily-upgrade.timer      apt-daily-upgrade.service
Thu 2025-01-02 07:31:21 UTC  8h left Wed 2025-01-01 16:47:44 UTC  6h ago       motd-news.timer              motd-news.service
Thu 2025-01-02 08:49:16 UTC  9h left Wed 2025-01-01 22:10:05 UTC  1h 17min ago certbot.timer                certbot.service

4 timers listed.
  • NEXT: When the timer is scheduled to trigger next.
  • LEFT: The time remaining until the next activation.
  • LAST: When the timer was last activated.
  • PASSED: How much time has passed since the last activation.

Here the systemd-tmpfiles-clean.service runs every day at 5:58 UTC. This means the timer systemd-tmpfiles-clean.timer is set to activate every 24 hours.

tmp Directory History

To dig deeper into the actual cleanup process, we can use journalctl to examine the logs for the systemd-tmpfiles-clean.service. This service is responsible for performing the cleanup of temporary files as per the defined rules.

journalctl -u systemd-tmpfiles-clean.service
-- Logs begin at Sat 2024-01-20 20:47:33 UTC, end at Wed 2025-01-01 23:27:33 UTC. --
Jan 21 18:31:10 blog.wittcode.com systemd[1]: Starting Cleanup of Temporary Directories...
Jan 21 18:31:10 blog.wittcode.com systemd[1]: Started Cleanup of Temporary Directories.
Jan 22 18:31:35 blog.wittcode.com systemd[1]: Starting Cleanup of Temporary Directories...
Jan 22 18:31:35 blog.wittcode.com systemd[1]: Started Cleanup of Temporary Directories.
Jan 23 18:31:40 blog.wittcode.com systemd[1]: Starting Cleanup of Temporary Directories...
Jan 23 18:31:40 blog.wittcode.com systemd[1]: Started Cleanup of Temporary Directories.
Jan 24 18:32:35 blog.wittcode.com systemd[1]: Starting Cleanup of Temporary Directories...
Jan 24 18:32:35 blog.wittcode.com systemd[1]: Started Cleanup of Temporary Directories.
Jan 25 18:33:35 blog.wittcode.com systemd[1]: Starting Cleanup of Temporary Directories...

This command retrieves log entries specifically associated with the systemd-tmpfiles-clean.service. Here we can see the timestamp for each cleanup event.

Configuring the systemd-tmpfiles-clean.service

The actual behavior of the tmp directory cleaning service is configurable. There are a few directories where this configuration is written, though a common one is /usr/lib/tmpfiles.d/tmp.conf.

cat /usr/lib/tmpfiles.d/tmp.conf 
#  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.

# See tmpfiles.d(5) for details

# Clear tmp directories separately, to make them easier to override
D /tmp 1777 root root -
#q /var/tmp 1777 root root 30d

# Exclude namespace mountpoints created with PrivateTmp=yes
x /tmp/systemd-private-%b-*
X /tmp/systemd-private-%b-*/tmp
x /var/tmp/systemd-private-%b-*
X /var/tmp/systemd-private-%b-*/tmp

# Remove top-level private temporary directories on each boot
R! /tmp/systemd-private-*
R! /var/tmp/systemd-private-*

The syntax used in this file is specifically for systemd-tmpfiles configuration and follows a structured format for defining rules on temporary file management. It's straightforward yet powerful, allowing for granular control over how temporary files and directories are handled. This is a default configuration and nothing special. To find out more information on the syntax used in this file, run the following command:

man tmpfiles.d
Is Your tmp Directory Working?