{"canonical_url":"https://engineer.company/glossary/daemon/","content_markdown":"# Daemon\n\n## What it is\n\nA daemon is a long-lived process that no person starts by hand: the operating\nsystem or a service manager launches it, it holds no terminal, and it stays\nalive to answer requests or keep watch over some part of the machine. The plural\nis daemons, and many of them carry the word in their name as a trailing letter\nd: `sshd` accepts SSH connections, `crond` runs scheduled jobs,\n`systemd-journald` gathers logs. Windows calls the same kind of program a\nservice, and so does systemd, which supervises most daemons on Linux today.\n\nThe traditional Unix recipe, which the Linux `daemon(7)` manual still sets out\nin fifteen numbered steps, has the program detach itself. It closes inherited\nfile descriptors, resets signal handlers, forks, calls `setsid()` to leave the\nterminal's session, and forks again so that it can never reacquire a terminal.\nThe first child exits so that PID 1 adopts the survivor, which points its\nstandard streams at `/dev/null`, moves to `/`, writes a PID file and drops\nprivileges. The C library's `daemon()` call does part of this, and the manual\nadvises against it because it implements only a subset of the steps.\n\nThe new-style daemon described in the same manual, the kind systemd and Apple's\nlaunchd both expect, turns the recipe inside out. The program stays in the\nforeground, because the supervisor already started it detached, with a clean\nenvironment, the right user and the right working directory. It writes its log\nlines to standard error, where the service manager collects them, says when it\nis ready to serve, exits cleanly on SIGTERM and reloads its configuration on\nSIGHUP. Apple's guide to launchd jobs is blunt about the old habit: a job must\nnot daemonize itself, and if it forks and exits, launchd concludes that the\nprocess has died.\n\nTwo neighbours get confused with it. A scheduled job, run by cron or a systemd\ntimer, starts, does its work and exits, whereas a daemon stays. And macOS splits\nthe word in two: a launch daemon serves the whole system and is loaded at boot,\nwhile a launch agent belongs to one user and runs only while that user is logged\nin. Linux draws the same line with systemd's per-user instances.\n\n## Why it matters\n\nEach daemon on a server is a standing commitment. It holds memory whether or not\nanyone uses it, and somebody has to patch it whenever its vendor ships a fix.\nAnd if it listens on a network port, it is part of the attack surface for every\nhour it runs. So the useful question is rarely which daemon to install; it is\nwhether the work needs a resident process at all.\n\nThree shapes cover most work. Something that happens on a schedule, such as a\nbackup, a report or a certificate renewal, needs no resident process: a timer\nstarts it, it runs to completion, and between runs it costs nothing. Something\nthat answers occasional requests can be socket-activated: the service manager\nowns the listening socket and starts the daemon on the first connection; the\nsocket stays bound across restarts, so incoming requests queue instead of being\nrefused. Only work that has to react continuously, a web server, a database, a\nmail transfer agent, earns a process that is always up. On a small machine that\nchoice decides whether everything fits in memory.\n\nA second decision comes with every third-party daemon headed for production, and\nfour questions settle it. Will it stay in the foreground when asked to? Does it\nlog to standard error, or insist on log files of its own? Can it tell the\nsupervisor that it is ready, rather than merely started? And does it need root,\nor can the service manager hand it the one privileged thing it needs, such as a\nsocket on port 443? One that answers all four well is cheap to run for years;\none that fails them arrives with a wrapper script, its own log rotation and an\nexception in the next security review.\n\n## What it looks like in practice\n\nServing the site over Gemini and Gopher cost two resident processes, not a\nsecond build. When\n[the site was mirrored to both protocols](/portfolio/mirrored-the-site-to-gemini-and-gopher/),\nthe build that writes the HTML also wrote 777 Gopher menus and the same number\nof Gemini documents at the recorded implementation stage, and two small daemons\non the same host took on serving them. A separate build comparison recorded\nzero HTML bytes changed when the output formats were added; that result does\nnot describe later editorial or visual changes.\n\nThe Tor onion mirror shows the network side: its daemon builds outbound\ncircuits, so nothing dials in and no firewall port opens.\n[The role that sets up the onion mirror](/portfolio/published-a-readable-tor-onion-mirror/)\ninstalls the daemon from the distribution's own package, writes a configuration\nwith the outbound proxy port switched off, and declines to overwrite an identity\nit did not generate. One step taught a general lesson: the key landed correctly,\nthe converge reported no change, and the new address went unpublished, because\nat that point nothing in the configuration named the new directory. The role now\nfails whenever an identity sits on disk with no service naming it.\n\nThe reporting shows what a quiet daemon can hide.\n[Six read-only collectors that render the host as Markdown](/portfolio/made-six-collectors-state-what-they-skipped/)\nwere reworked once it was clear that a blind collection and a healthy host\nprinted the same page: a mirror whose daemon changed its log format reported the\nsame zero as a mirror nobody visited. The collectors now report, among other\nthings, journal lines read against journal lines recognised, so a daemon that\nchanges what it writes shows up as a gap rather than as calm.\n\n## Where it goes wrong\n\n**The service starts and stops again a second later.** An old-style daemon forks\nand lets its parent exit, as the recipe says. Under systemd's default\n`Type=simple`, the process systemd launched is the main process, so its exit\nmeans the service has stopped, and with the default `KillMode=control-group` the\nchild that was meant to carry on is killed along with everything else in the\nunit's control group. Run the program with its foreground flag; for the few that\ncannot be changed, declare `Type=forking` with `PIDFile=`, a type the manual\ndiscourages for new code.\n\n**Clients connect too early and are refused.** With `Type=simple`, systemd\ntreats the service as started the moment it has forked the process, so units\nordered after it start while the daemon is still reading its configuration. The\nservice manual adds that `systemctl start` reports success under that type even\nwhen the binary is missing or the configured user does not exist. Prefer\n`Type=exec`, which waits until the binary has really been executed, and for\nanything others depend on, let the daemon announce readiness itself under\n`Type=notify`:\n\n```c\n#include \u003csystemd/sd-daemon.h\u003e\n\nint main(void) {\n    load_config();\n    open_listeners();\n    sd_notify(0, \"READY=1\"); /* units ordered after this one start now */\n    return serve_forever();\n}\n```\n\n**A stop script kills a stranger.** A forking daemon writes its process ID to a\nfile so that scripts can signal it later. After a crash the file stays behind,\nthe kernel hands the same number to an unrelated process, and the next stop\nsends SIGTERM to the wrong program. The service manual's advice is to avoid PID\nfiles in modern projects altogether. A supervisor that started the process\ntracks it through its control group and has no file to go stale.\n\n**A daemon dies at startup and leaves no trace.** The old recipe points standard\noutput and error at `/dev/null`, so an error raised before the daemon opens its\nown log file disappears. Under a supervisor, leave both streams alone: whatever\nthe daemon writes to standard error lands in the journal, where `journalctl -u`\nreads it unit by unit.\n\n**One exploited bug hands over the whole machine.** A daemon that runs as root\nbecause it needed port 80 at startup keeps root for every request it serves\nafterwards. Let the service manager bind the privileged socket and pass it in,\nand leave privilege dropping to the supervisor, as `daemon(7)` recommends;\nsettings such as `NoNewPrivileges=` and `ProtectSystem=` narrow what a\ncompromised process can touch.\n\n## Further reading\n\n- [daemon(7)](https://man7.org/linux/man-pages/man7/daemon.7.html) — the fifteen\n  steps of the classic recipe, and what a new-style daemon does instead.\n- [daemon(3)](https://man7.org/linux/man-pages/man3/daemon.3.html) — the C\n  library shortcut, and the session-leader flaw its own manual records.\n- [sd_notify(3)](https://man7.org/linux/man-pages/man3/sd_notify.3.html) — how a\n  daemon tells systemd that it is ready, reloading or stopping.\n- [systemd.socket(5)](https://man7.org/linux/man-pages/man5/systemd.socket.5.html)\n  — socket activation, where the manager holds the port and starts the daemon on\n  demand.\n- [setsid](https://pubs.opengroup.org/onlinepubs/9799919799/functions/setsid.html)\n  — the POSIX definition of the call that detaches a process from its terminal.\n- [Creating Launch Daemons and Agents](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html)\n  — Apple's rules for launchd jobs, including why one must never fork and exit.\n\n\u003chttps://engineer.company/glossary/daemon/\u003e\n","date_modified":"2026-11-08T02:22:23Z","description":"How daemons are started, supervised and stopped on Linux and macOS, when a job deserves one at all, and the mistakes that leave a service dead or silent.","formats":{"gemtext":"https://engineer.company/glossary/daemon/index.gmi","gopher":"https://engineer.company/glossary/daemon/gophermap","html":"https://engineer.company/glossary/daemon/","json":"https://engineer.company/glossary/daemon/index.json","markdown":"https://engineer.company/glossary/daemon/index.md","pdf":"https://engineer.company/glossary/daemon/index.pdf"},"kind":"page","language":"en","title":"What is a daemon?","url":"https://engineer.company/glossary/daemon/","version":1}