• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

How systemd-tmpfiles cleans up /tmp/ or /var/tmp (replacement of tmpwatch) in CentOS / RHEL 7

by admin

In CentOS/RHEL 7, tmpfiles cleans up /tmp or /var/tmp by removing unused files. This function was called “tmpwatch” in CentOS/RHEL 6 which is invoked by crond, however, it is now realized by timer of systemd on CentOS/RHEL 7.

Currently the detailed functions of tmpfiles are described in configuration files:

/usr/lib/systemd/system/systemd-tmpfiles-clean.timer
/usr/lib/systemd/system/systemd-tmpfiles-clean.service
/usr/lib/tmpfiles.d/tmp.conf

Shortly speaking, the functions available are:

  • removing files/directories in /tmp un-accessed more than 10 days(defined in tmp.conf)
  • removing files/directories in /var/tmp un-accessed more than 30 days(defined in tmp.conf)
  • there are several files which will not be removed(defined in tmp.conf)
  • the removing command is “/usr/bin/systemd-tmpfiles –clean“(defined in systemd-tmpfiles-clean.service)

“un-accessed” is decided by checking all of atime/mtime/ctime of the file/directory. Thus, in case even one of atime/mtime/ctime of a file in /tmp/ is newer than 10 days, the file will not removed. If a file/directory in /tmp/ is not removed by tmpfiles even if it seems it is older than 10 days, the reason can be checked by running a command with debug options like below manually:

# SYSTEMD_LOG_TARGET=console SYSTEMD_LOG_LEVEL=debug /usr/bin/systemd-tmpfiles --clean

For example, below intends that a directory “/tmp/latest” can not be removed due to its atime.

# SYSTEMD_LOG_TARGET=console SYSTEMD_LOG_LEVEL=debug /usr/bin/systemd-tmpfiles --clean 2>&1 | grep latest
Directory "/tmp/latest": access time Wed 2017-12-06 16:56:28.771577 IST is too new

Sample configuration files

Below are the 3 sample configuration files (un-edited) with their default settings.

# cat /usr/lib/systemd/system/systemd-tmpfiles-clean.timer
[Unit]
Description=Daily Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)

[Timer]
OnBootSec=15min
OnUnitActiveSec=1d
# cat /usr/lib/tmpfiles.d/tmp.conf
# Clear tmp directories separately, to make them easier to override
v /tmp 1777 root root 10d
v /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
# cat /usr/lib/systemd/system/systemd-tmpfiles-clean.service
[Unit]
Description=Cleanup of Temporary Directories
Documentation=man:tmpfiles.d(5) man:systemd-tmpfiles(8)
DefaultDependencies=no
Conflicts=shutdown.target
After=systemd-readahead-collect.service systemd-readahead-replay.service local-fs.target time-sync.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/usr/bin/systemd-tmpfiles --clean
IOSchedulingClass=idle

Filed Under: CentOS/RHEL 7, Linux

Some more articles you might also be interested in …

  1. sshpass Command: Non-interactive Password Authentication with SSH
  2. How to encrypt traffic between CUPS servers
  3. grub-install Command Options
  4. How to disable a specific command for a specific user in Linux
  5. vshadowmount: command not found
  6. CentOS / RHEL 7 : How to change runlevels (targets) with systemd
  7. How to block non-root user from creating crontab entry in Linux
  8. Linux OS Service ‘iptables’
  9. nohup Command Options in Linux
  10. CentOS / RHEL 6,7 : How to enable or disable XDMCP service (GDM)

You May Also Like

Primary Sidebar

Recent Posts

  • vgextend Command Examples in Linux
  • setpci command – configure PCI device
  • db_load command – generate db database
  • bsdtar command – Read and write tape archive files

© 2022 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright