Understanding and Preventing /var/run Directory Cleanup on Fedora Linux During Reboots


2 views

When working on Fedora systems (specifically FC18 in this case), you might notice that directories under /var/run mysteriously disappear after reboots. This is particularly frustrating when your application (like the smf-sav milter mentioned) needs persistent runtime data in this location.

Fedora (and most modern Linux distributions) use systemd-tmpfiles to manage temporary files. The cleanup is controlled by configuration files in /usr/lib/tmpfiles.d/ and /etc/tmpfiles.d/.

Here's the default behavior for /var/run in Fedora:

# From /usr/lib/tmpfiles.d/var.conf
d /var/run 0755 root root -

The dash (-) at the end means the directory should be cleared at boot.

To prevent your /var/run/smf-sav/ directory from being cleaned up, create a custom tmpfiles configuration:

# Create a new configuration file
echo "d /var/run/smf-sav 0755 root root -" | sudo tee /etc/tmpfiles.d/smf-sav.conf

# Apply the changes immediately (optional)
sudo systemd-tmpfiles --create

For more complex scenarios, you might want to create a systemd service that ensures directory existence:

[Unit]
Description=Create smf-sav runtime directory
Before=smf-sav.service

[Service]
Type=oneshot
ExecStart=/usr/bin/mkdir -p /var/run/smf-sav
ExecStart=/usr/bin/chown root:root /var/run/smf-sav
ExecStart=/usr/bin/chmod 755 /var/run/smf-sav
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

After implementing either solution, test it with:

# Dry-run to see what would happen
sudo systemd-tmpfiles --clean --dry-run

# Check if the directory survives reboot
sudo touch /var/run/smf-sav/testfile
sudo reboot
ls -la /var/run/smf-sav/

While we've solved the immediate problem, consider these architectural questions:

  • Should this data persist across reboots? Maybe /var/lib would be more appropriate
  • Are you following the Filesystem Hierarchy Standard properly?
  • Could your application handle missing directories at startup?

On Fedora systems (and most modern Linux distributions), the contents of /var/run are automatically cleared during system reboots. This behavior is actually by design, not a bug. The directory is meant for temporary runtime files that should not persist across reboots.

Fedora uses systemd's tmpfiles.d mechanism to manage temporary directories. The cleanup is configured in:

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

These configuration files specify which directories should be cleaned and how.

To make your /var/run/smf-sav directory persistent, create a custom configuration file:

# Create a new tmpfiles.d configuration
echo "d /var/run/smf-sav 0755 root root -" | sudo tee /etc/tmpfiles.d/smf-sav.conf

# Apply the changes immediately
sudo systemd-tmpfiles --create

The configuration line breakdown:

  • d - creates a directory
  • /var/run/smf-sav - path to directory
  • 0755 - permissions
  • root root - owner and group
  • - - no automatic cleanup

For data that truly needs persistence, consider using /var/lib instead:

sudo mkdir -p /var/lib/smf-sav
sudo chown smf-sav:smf-sav /var/lib/smf-sav
sudo chmod 750 /var/lib/smf-sav

After implementing either solution, test by rebooting:

sudo reboot

Then check if your directory persists:

ls -ld /var/run/smf-sav

For more robust handling, you might want to add directory creation logic in your application's startup script:

#!/bin/bash

RUNTIME_DIR="/var/run/smf-sav"
if [ ! -d "$RUNTIME_DIR" ]; then
    mkdir -p "$RUNTIME_DIR"
    chown smf-sav:smf-sav "$RUNTIME_DIR"
    chmod 750 "$RUNTIME_DIR"
fi

# Start your application
exec /usr/sbin/smf-sav