How to Persistently Create a Custom Directory in /var/run on Linux Boot


2 views

Many Linux daemons need to store runtime files like PID files in /var/run. However, this directory gets wiped on every reboot as it's meant for temporary runtime files. When you manually create a subdirectory like /var/run/mydaemon and set permissions:

mkdir /var/run/mydaemon
chown myuser:myuser /var/run/mydaemon

These changes disappear after reboot, breaking your daemon's functionality.

For systemd-based systems (most modern Linux distributions), you have several robust approaches:

1. Using tmpfiles.d

Create a configuration file in /etc/tmpfiles.d/:

# /etc/tmpfiles.d/mydaemon.conf
d /var/run/mydaemon 0755 myuser myuser - -

This will recreate the directory with correct permissions at boot. The format is:

d [path] [mode] [owner] [group] [age] [argument]

2. Systemd Service Unit Modification

If your daemon uses systemd, add these directives to your service file:

[Service]
RuntimeDirectory=mydaemon
RuntimeDirectoryMode=0755
User=myuser
Group=myuser

Systemd will automatically create /run/mydaemon (symlinked to /var/run/mydaemon) with correct permissions.

3. Legacy rc.local Approach

For older systems without systemd, you can use /etc/rc.local:

#!/bin/sh
mkdir -p /var/run/mydaemon
chown myuser:myuser /var/run/mydaemon
exit 0

Remember to make it executable:

chmod +x /etc/rc.local

After implementing any solution, test it with:

sudo systemd-tmpfiles --create
ls -ld /var/run/mydaemon

Or simply reboot and check if the directory persists.

When creating directories in /var/run:

  • Use minimal required permissions (usually 0755)
  • Never use root as owner unless absolutely necessary
  • Consider using private tmp directories for sensitive data

Many Linux daemons require a dedicated directory in /var/run for storing PID files or other runtime data. However, this directory is typically cleared on each system reboot as it's meant for temporary files. When you manually create a directory like /var/run/mydaemon and set permissions:

# mkdir /var/run/mydaemon
# chown myuser:myuser /var/run/mydaemon

You'll find it disappears after reboot. This happens because /var/run is usually mounted as tmpfs (a temporary filesystem in RAM).

For systems using systemd (most modern Linux distributions), there are several robust approaches:

1. Using tmpfiles.d

The preferred method is to create a configuration file in /etc/tmpfiles.d/:

# /etc/tmpfiles.d/mydaemon.conf
d /var/run/mydaemon 0755 myuser myuser -

This will create the directory at boot with specified permissions. The format is:

  • d - indicates a directory
  • /var/run/mydaemon - path to create
  • 0755 - permissions
  • myuser myuser - owner and group
  • - - no age-based cleanup

2. Systemd Service Unit

If your daemon uses a systemd service, you can add an ExecStartPre directive:

[Unit]
Description=My Daemon Service

[Service]
ExecStartPre=/usr/bin/mkdir -p /var/run/mydaemon
ExecStartPre=/usr/bin/chown myuser:myuser /var/run/mydaemon
ExecStart=/usr/sbin/mydaemon
User=myuser
Group=myuser

[Install]
WantedBy=multi-user.target

For older systems using SysVinit, you can add the commands to your init script:

start() {
    # Create directory if it doesn't exist
    if [ ! -d "/var/run/mydaemon" ]; then
        mkdir -p /var/run/mydaemon
        chown myuser:myuser /var/run/mydaemon
    fi
    
    # Start the daemon
    echo -n "Starting mydaemon: "
    daemon /usr/sbin/mydaemon
    echo
}

After implementing any of these solutions, test by rebooting and checking:

$ ls -ld /var/run/mydaemon
drwxr-xr-x 2 myuser myuser 40 Mar 1 10:00 /var/run/mydaemon

Remember that /var/run is often symlinked to /run on modern systems, so you might want to use /run/mydaemon instead for consistency.