Daemons

Daemons

A daemon is a long-lived background process. No controlling terminal. Detached from the user's session. Runs forever (or until told to stop), serving requests, doing work, holding state.

The trailing d is convention: cron, sshd, httpd, inetd, udevd, dbus-daemon, mxr-daemon, lazydap-daemon. Pronounced "demon" (the Greek δαίμων, helpful spirit). MIT's CTSS hackers in the 1960s named them; the term predates Unix.

Why daemons exist

Two problems daemons solve:

  1. Persistent state across short-lived client invocations. A CLI command runs and exits in milliseconds. If every invocation had to load 100MB of email cache from disk, parse it, do its thing, then exit, it would be unusably slow. A daemon keeps that state in RAM between invocations.
  2. Background work. Periodic syncing, listening for incoming connections, watching files for changes — none of these can happen if there's no process running.

A daemon is the answer to "how do I have stateful, always-on behaviour on a system that's organised around short-lived processes."

Properties of a well-behaved daemon

Conventions accumulated over five decades of Unix:

How daemons get started

1. System-level (init / systemd / launchd)

The OS starts the daemon at boot, supervises it, restarts on crash. systemd unit files, launchd plists, OpenRC scripts. The daemon is "always there."

Examples: sshd, cron, dbus-daemon, nginx in production.

2. User-level (systemd --user, launchctl)

Same idea but per-user, started at login. The daemon runs as the user, not root.

Examples: dbus-daemon per-user, pulseaudio, IDE language servers.

3. Auto-spawned by clients (the mxr/lazydap pattern)

The daemon doesn't run constantly. The first CLI command that needs it forks the daemon, then connects. See The Local Daemon Pattern.

Trade-off: cold-start latency on first invocation (~50ms typical) in exchange for not consuming resources when idle.

4. Manually started

Old-school: user runs daemon & and walks away. Common in development.

What's NOT a daemon

Daemon vs service vs server

The terms blur. Rough Unix distinctions:

In modern usage these are mostly synonymous, but if you say "daemon" Unix people expect the detachment behaviours.

What mxr and lazydap do

Both Mxr and Lazydap use the auto-spawning daemon pattern: the user runs mxr search ... and the binary checks for a running daemon (via socket probe), forks one if absent, drops a PID file, binds a Unix socket, and the CLI client connects.

The daemon owns:

Clients (CLI subcommands, TUI, agent skill) are stateless. They open the socket, send a request, read the response, close. The daemon survives between invocations.

This is a deliberate inversion of "everything is a one-shot CLI" — a hybrid that keeps the CLI's UX (composable shell commands) with the daemon's performance (no cold-start cost per command).

Common pitfalls

See also