Linux Daemons
A daemon is a background process that runs continuously without a controlling terminal. When you start nginx, sshd, or cron, you're starting daemons. They're the engines keeping your system running.
What Makes a Daemon Different?
How is a daemon different from a regular process?
Three key properties: it runs in the background (no terminal interaction), it has no controlling terminal (so keyboard signals like Ctrl+C don't affect it), and it typically runs as a service user (not root, not your user). Daemons usually have names ending in "d": sshd, nginx, mysqld, systemd.
How Traditional Daemons Are Created
Before systemd, programs had to daemonize themselves manually. The classic pattern:
Classic daemonize() steps:
1. fork() → parent exits (shell regains control)
2. Child calls setsid() → becomes session leader,
creates new session, detaches from terminal
3. fork() again → second child can't acquire a terminal
(not a session leader anymore)
4. chdir("/") → don't hold a mounted filesystem
5. umask(0) → don't inherit restrictive permissions
6. Close stdin/stdout/stderr (fd 0, 1, 2)
7. Reopen them to /dev/null
Result: a process completely detached from user session
This is complex and error-prone. Many daemons did it differently, causing problems.
The systemd Way — Much Simpler
How does systemd simplify daemon creation?
You don't daemonize at all. With systemd, write your program as a simple foreground application that reads from stdin and writes to stdout/stderr. systemd handles all the detachment, logging, and lifecycle management.
# /etc/systemd/system/myapp.service
[Service]
Type=simple # Just run foreground — no daemonizing
ExecStart=/opt/myapp/myapp
StandardOutput=journal # stdout goes to journald
StandardError=journal
Restart=on-failure
Common System Daemons
| Daemon | Purpose | Config location |
|---|---|---|
| sshd | SSH server — accepts remote logins | /etc/ssh/sshd_config |
| nginx / apache2 | Web server | /etc/nginx/ or /etc/apache2/ |
| cron / crond | Scheduled jobs | /etc/crontab, /etc/cron.d/ |
| systemd-journald | Logging | /etc/systemd/journald.conf |
| NetworkManager | Network configuration | /etc/NetworkManager/ |
| udevd | Device management | /etc/udev/rules.d/ |
# List all running services
systemctl list-units --type=service --state=running
# Check if a daemon is running
systemctl is-active sshd
# View daemon logs
journalctl -u sshd --since "1 hour ago"