Skip to content

Daemon

A daemon is a background process that runs continuously, detached from any controlling terminal, providing a service or performing scheduled work — started by init, systemd or launchd at boot.

Definition

What it is

A daemon has no terminal. It does not read from stdin, does not write to stdout unless redirected, and does not die when the user logs out. It starts at boot (via init, systemd, launchd, or Windows Service Control Manager), runs for hours, days or years, and is restarted if it crashes. Common daemons: sshd (SSH server), cron (scheduler), nginx (web server), mysqld (MySQL), systemd-journald (log collector), dockerd (Docker).

The daemonizing process is a sequence: fork, setsid, fork again (to prevent reacquiring a terminal), chdir to root (or a fixed dir), reset umask, close inherited file descriptors, redirect 0/1/2 to /dev/null. Modern systems use systemd's Type=simple or Type=forking to manage this without the process daemonizing itself.

Why it matters

The whole architecture of Unix networking depends on daemons. When you visit example.com, the server running nginx is a daemon, started at boot, independent of any user. When you SSH into a box, sshd listens on port 22 as a daemon. When you cron a job, crond runs it as a daemon. Without daemons, every service would require a human logged in with a terminal open — the opposite of a server.

Daemon vs service vs cron vs process

Aspect

Daemon

Service

Cron

Process

Runs continuously

Yes

Yes

No (periodic)

Any duration

Detached from TTY

Yes

Yes

N/A

No

Managed by

init/systemd

systemd/launchd

cron daemon

Kernel

Typical example

sshd, nginx

same ones

crond jobs

Any program

Starts at boot

Yes

Yes

Yes

No

The daemon lifecycle

graph LR
  B[Boot] --> S[systemd]
  S --> D[Daemon process]
  D --> R[Run, serve]
  R --> C{Crash?}
  C -->|Yes| S
  C -->|No| R
  style D fill:#E8F5E9
  style S fill:#FFF3E0

Writing a daemon

Modern daemon rules

  1. Prefer systemd

    Type=simple lets systemd manage the process. No daemonizing code needed. The process just runs in the foreground.

  2. Fork twice if you must daemonize

    fork → parent exits → setsid → fork again → grandparent exits. Child has no TTY, is a session leader.

  3. Chdir to /

    A daemon should not keep the cwd pinned to a filesystem; that prevents unmounting.

  4. Reset umask

    umask(0) so the daemon's file creation is not inherited from the shell.

  5. Redirect stdio

    freopen('/dev/null', 'r', stdin); freopen('/dev/null', 'w', stdout); freopen('/dev/null', 'w', stderr);

  6. Log to syslog or journald

    Not to stdout. Logs need to survive the daemon's restart.

Common failures

Origin

In 1867, physicist James Clerk Maxwell imagined a "demon" that could sort molecules by speed, violating the second law of thermodynamics (it cannot, if you account for its own entropy). In 1963, MIT's Project MAC (Multics precursor) adopted "daemon" for background processes that sort and dispatch tasks — the metaphor was Maxwell's sorting demon. The term survived into Unix (1969), where daemons were the standard name for long-running background processes. The Devil-in-the-machine connotation is intentional but benign: the daemon is a helpful spirit, not a malevolent one.

Daemons are processes that are not associated with a terminal and are usually started at boot time. The name comes from Maxwell's demon, a thought experiment in physics.

— Unix System Administration Handbook (Nemeth et al.), 4th ed., 2010
1867 · Maxwell imagina su demonio
1963 · Project MAC acuña "daemon" para procesos de fondo
1969 · Unix adopta el término
2010 · systemd reduce necesidad de self-daemonize
2 · forks clásicos para daemonizar

Evidence

FAQ

How do I identify a daemon?

Process has no controlling TTY (check with ps -o tty), often ends in d (sshd, crond, httpd), started by init.

Why the "d" suffix?

Convention from Unix: sshd is the daemon for ssh; httpd is the daemon for http. mysqld is the MySQL daemon.

Did systemd kill daemons?

No. It changed how they are managed: the process runs in the foreground, systemd handles the daemonizing. The concept is the same; the code is simpler.

Daemon vs container?

A container runs one foreground process. The orchestrator (Kubernetes) acts as the daemon manager. Same idea, different vocabulary.

Connections

Etymology

Origin: metaphor

  • daemon (noun): from Greek daimon, "spirit, divine power"; via Maxwell's demon (1867) and Project MAC (1963)

Semantic network

Daemonunixoperating-CronsystemdserviceprocessInit

● broader ● narrower ● related

Broader

  • unix
  • operating-systems

Related

Used in the garden

Sources