In one line: systemd is Linux's PID 1 init, unifying service management, dependencies, logging, timers, and cgroup resource limits. The standard way to deploy a production service is to write a service unit.
What it is#
Minimal service unit at /etc/systemd/system/myapp.service:
[Unit]
Description=My App
After=network.target
[Service]
Type=simple
User=myapp
WorkingDirectory=/opt/myapp
ExecStart=/opt/myapp/bin/serve
Restart=on-failure
RestartSec=5
Environment="PORT=8080"
LimitNOFILE=65536
[Install]
WantedBy=multi-user.targetsudo systemctl daemon-reload
sudo systemctl enable --now myapp
journalctl -u myapp -fAnalogy#
systemd is the building's facilities team: decides which tenants open when (startup order), how to recover from a power cut (auto-restart), how much electricity each can draw (cgroups), and what security records to keep (journal).
Key concepts#
Common commands#
systemctl status nginx
systemctl start / stop / restart / reload nginx
systemctl enable --now nginx # boot-enable + start now
systemctl disable --now nginx
systemctl daemon-reload # required after editing unit files
systemctl list-units --type=service
systemctl edit nginx # create drop-in override
journalctl -u nginx -f
systemd-analyze blame # which units slow bootHow it works#
systemd uses cgroups to track every child a service forks — no process can escape.
Practical notes#
-
Production must-haves:
Restart=on-failure+RestartSec=+LimitNOFILE=+ a dedicatedUser=. -
Security sandbox fields are free wins:
ProtectSystem=strict,ProtectHome=true,PrivateTmp=true,NoNewPrivileges=true,CapabilityBoundingSet=— reduces blast radius after a compromise. -
Resource limits:
MemoryMax=2G,CPUQuota=50%— more modern than ulimit. -
Timers replace cron:
# /etc/systemd/system/backup.timer [Timer] OnCalendar=*-*-* 03:00:00 Persistent=true [Install] WantedBy=timers.target -
systemd-cgtop: top-style view of per-service resource usage. -
Socket activation: listen on a port → service spins up only on first connection, saves memory.
Easy confusions#
Production standard.
No restart on crash, scattered logs, unobservable.
Further reading#
- Log system
- cron
- Docker — containers don't usually run systemd inside