skip to content

On a Linux system, what is /run for, and why does a daemon whose installation script does `mkdir /run/myservice` stop working after the first reboot?

level: seniorimportance: should knowfreq 40%

answer

  1. state that describes this boot only
  2. mounted before /var is available
  3. empty every time the machine starts
  4. stale PID files designed out of existence
  5. declared at boot, never installed once

basics

~20 s

/run holds runtime state valid only since the current boot — PID files, Unix sockets, lock files. It is a memory-backed filesystem mounted early and empty at every boot, so a directory created once at install time is gone after a restart and must be recreated during startup instead.

solid answer

~40 s

`/run` is the FHS location for data describing the *running* system: PID files, Unix domain sockets, lock files, per-user runtime directories under `/run/user/<uid>`. It is mounted as a memory-backed filesystem very early in boot — before `/var` is necessarily available — and it starts empty every time. That is a feature: stale PID files and sockets from a crashed process can never survive a reboot and confuse the next start. The consequence for packaging is that `/run/myservice` cannot be created once at install time; nothing persists it. The directory, with the right owner and mode, must be recreated on every boot, either by the service manager as part of starting the unit or by a `systemd-tmpfiles` configuration. `/var/run` and `/var/lock` still exist on current systems only as compatibility symlinks into `/run` and `/run/lock`.

go deeper

for a junior

Know that /run holds PID files and sockets for the currently running system, lives in memory, and is empty after every boot, so nothing you leave there survives a restart.

for a middle

Explain why it was split out of /var — availability before /var is mounted — and why an install-time mkdir cannot work. Name the boot-time declaration mechanisms and the need for correct owner and mode.

for a senior

Argue the correctness case: stale PID files and sockets after an unclean shutdown, why wiping the directory removes that class of bug outright, and how you would diagnose a daemon that starts fine on install but fails on the first reboot.

for a principal

Own the packaging convention across a fleet: runtime paths are declared, never installed; ownership and mode are part of the declaration; and services must tolerate a fresh, empty /run on every start without hand-holding from an operator.

## What /run is `/run` holds *runtime variable data* — information about the running system that is valid only for the lifetime of this boot. Typical contents: - **PID files**: `/run/sshd.pid` and friends, recording the process ID of a running daemon. - **Unix domain sockets**: the control and IPC endpoints daemons expose to local clients. - **Lock files**: `/run/lock`, previously `/var/lock`. - **Per-user runtime directories**: `/run/user/<uid>`, created for each logged-in user and pointed to by `XDG_RUNTIME_DIR`, mode 0700, torn down when the user's last session ends. - **Early-boot and system state** written by the service manager and by `udev`. It is mounted as a memory-backed filesystem, and it is empty at every boot. ## Why it exists as a separate directory Before `/run`, this data lived in `/var/run` and `/var/lock`. That created a bootstrapping problem: `/var` may be a separate filesystem, mounted comparatively late, possibly after services that already need somewhere to put a socket. `/run` was introduced to be available almost immediately, independent of the disk layout, and independent of whether `/var` is even writable — which matters for systems with a read-only or network-mounted `/var`. The compatibility path was kept: on current distributions `/var/run` is a symlink to `/run` and `/var/lock` a symlink to `/run/lock`, so software that still writes the old paths lands in the right place. ```sh findmnt /run # a memory-backed mount, not part of the root filesystem readlink -f /var/run # resolves to /run ``` ## Why the emptiness is the point Because the filesystem lives in memory, its contents cannot survive a restart — and this is a correctness property, not just an efficiency one. A PID file is a claim: "process N is me". After an unclean shutdown, a stale PID file on persistent storage claims a process ID that either no longer exists or, worse, now belongs to something else entirely. Startup logic that reads it and concludes "already running", or that sends a signal to that PID, is now wrong in a way that ranges from an annoying refusal to start to signalling an innocent process. Because `/run` is wiped by construction, that entire family of stale-state bugs is designed out. The same holds for leftover socket files, which would otherwise cause a bind failure on restart. ## Why `mkdir /run/myservice` at install time fails The installation script creates the directory on a filesystem that exists only until the machine restarts. On the next boot, `/run` is fresh and empty; `/run/myservice` is not there. The daemon then tries to create its PID file or socket in a directory that does not exist and fails at startup — typically with a not-found error and, confusingly, only ever *after a reboot*, never during testing on the machine where it was installed. There are also two related failure modes worth naming. If the daemon runs as an unprivileged user, it cannot create a directory directly under `/run` itself, because `/run` is root-owned — so "just have the daemon mkdir it" only works if the daemon still has privilege at that moment. And if the directory *is* created by whatever runs first, its owner and mode must be right, or the daemon drops privileges and then cannot write into its own runtime directory. ## The correct approach The directory must be declared as something the system creates **at every boot**, with an explicit owner, group and mode. Two standard mechanisms exist on systemd-based distributions: 1. **Let the service manager own it.** A unit can declare a runtime directory, which the manager creates with the specified ownership before the service starts and removes when it stops. This is the cleanest option because the lifetime matches the service exactly. 2. **Declare it in a `systemd-tmpfiles` configuration.** Packages ship a file under `/usr/lib/tmpfiles.d/` (administrators override under `/etc/tmpfiles.d/`) describing directories to create at boot, with mode and ownership. This is the right choice when the directory must exist independently of one particular unit. Either way, the packaging rule is: **anything under `/run` is declared, never installed.** A path that is created once is a path that exists exactly until the next reboot. ## The generalisation worth stating `/run` is the clearest example of the FHS's organising idea that a directory carries a *contract about lifetime*. `/usr` changes only on upgrade; `/etc` changes when an administrator edits it; `/var` persists across reboots; `/run` is defined to vanish. Software that treats them as interchangeable folders eventually breaks, and it breaks at boot — the least convenient moment and the hardest to reproduce from a shell on a machine that is already up.

  • What problem was /run introduced to solve that /var/run could not?
    `/var` may be a separate filesystem mounted comparatively late, or even read-only or network-backed, yet daemons need somewhere to place sockets and PID files almost immediately. `/run` is a memory-backed mount available very early and independent of the disk layout. `/var/run` and `/var/lock` remain as compatibility symlinks into `/run` and `/run/lock` so old paths still resolve.
  • Why is it a correctness benefit, not just a tidiness benefit, that /run is cleared at boot?
    Persistent PID files survive crashes and then claim process IDs that are dead or have been reused by an unrelated process. Startup logic that trusts them either refuses to start or signals the wrong process. Leftover socket files similarly cause bind failures on restart. Wiping the directory by construction removes that whole class of stale-state bug rather than papering over it.
  • What is /run/user/<uid> and what manages it?
    It is a per-user runtime directory, mode 0700 and owned by that user, pointed to by `XDG_RUNTIME_DIR`. It gives desktop and per-user services a private place for sockets and state. The session infrastructure creates it when the user's first session opens and removes it when the last one closes, so its contents never outlive the user's presence on the machine.

saying these in an interview costs you the question

  • Thinks /run is stored on disk like /var
  • Creates runtime directories once, at package install time
  • Says /var/run is a different directory from /run today
  • Keeps PID files on persistent storage to survive reboots
  • Assumes an unprivileged daemon can mkdir directly under /run

context