Skip to main content

Daemon

A daemon is a background process that runs without a controlling terminal and provides a service, usually started at boot. Examples include sshd, nginx, and cron.

Understanding Linux Daemons

What Is a Daemon in Linux?

A daemon is a long-running background process with no terminal attached. It starts at boot, waits for work (a connection, a schedule, an event), and keeps running after you log out. By convention many daemon names end in "d": sshd, crond, systemd-journald. Some, like nginx and postgres, do not follow that naming.

How Daemons Start: Old Way vs systemd

Traditional daemons detached themselves with a "double fork": fork, exit the parent, call setsid() to leave the terminal session, fork again, then redirect stdin, stdout, and stderr. With systemd none of that is needed. You write a unit file, systemd runs the program in the foreground, tracks its PID, restarts it, and captures its output in the journal.

stateDiagram-v2 [*] --> inactive inactive --> activating: systemctl start activating --> active: started OK active --> deactivating: stop or crash deactivating --> inactive: stopped deactivating --> activating: Restart policy

Common Production Daemons

TEXT
sshd SSH server
nginx Web server and reverse proxy
postgres PostgreSQL database server
redis-server Redis cache
dockerd Docker daemon
kubelet Kubernetes node agent
journald Log collection (part of systemd)
crond Job scheduler
fail2ban Brute-force protection
auditd Security audit logging

Practical Commands for Managing Daemons

Bash
systemctl list-units --type=service --state=running # running daemons
systemctl list-unit-files --type=service --state=enabled # start at boot
systemctl is-active nginx
systemctl is-enabled nginx
systemctl status nginx # PID, memory, recent logs
systemctl list-units --type=service --state=failed
journalctl -u nginx -f # follow logs
journalctl -u nginx --since today
sudo systemctl mask telnet.socket # block it from starting at all
sudo systemctl unmask telnet.socket

Troubleshooting Daemons

Symptom Command What to Look For
Will not start journalctl -u name -n 30 Startup error
High memory systemctl status name Memory line
Duplicate instances pgrep -c name Count above expected
Cannot bind port journalctl -u name | grep -i bind Port in use or denied
Tip

systemctl mask is stronger than disable. A disabled service will not start at boot but can still be started by another unit; a masked one is linked to /dev/null and cannot be started until unmasked.

Remember

A daemon has no terminal, so its output goes nowhere unless configured. Under systemd it lands in journald automatically. If a daemon seems silent, check where its output is being sent.

Frequently Asked Questions

How is a daemon different from a process started with &?

A job started with `&` is still tied to your shell session and can receive SIGHUP when the terminal closes. A daemon has its own session with no controlling terminal and is re-parented to PID 1, so it survives logout.

Should I still write my own double-fork daemon code?

Rarely. systemd supervises a foreground process with `Type=simple`, handles restarts and logging, and reaps zombies reliably, so hand-rolled daemonizing is mostly legacy practice.

What is the difference between disabling and masking a service?

Disabling only removes the boot-time link, so something else can still start it. Masking makes it impossible to start by any means until you unmask it.

Found a mistake, an outdated step or a broken link?