08
systemd Services
- Service not responding — status first
- Enable on boot vs start now
- restart vs reload; drop-ins survive package upgrades
Must-know cold
systemctl status UNIT·start·stop·restart·reloadsystemctl enable --now UNIT·is-active·is-enabledsystemctl cat UNIT·daemon-reloadafter unit editssystemctl reset-failed UNITafter crash loop fixed- Never first-move: restart without reading status/logs
Active vs enabled
Definition: Two independent axes. ActiveState = now. UnitFileState = how/whether it is hooked for boot. Masked is a UnitFileState that also blocks start.
| State | Axis | Meaning |
|---|---|---|
| active (running) | ActiveState | Service process is up |
| active (exited) | ActiveState | Still active (typical Type=oneshot + RemainAfterExit=) — no long-running PID |
| inactive (dead) | ActiveState | Not started |
| failed | ActiveState | Last start/run failed (crash, non-zero, timeout, start-limit) |
| enabled | UnitFileState | [Install] symlinks in place — usually start at boot |
| disabled | UnitFileState | No [Install] links. Can still start via socket/timer/dbus/Wants= |
| static | UnitFileState | No [Install] section — cannot enable; pulled in by deps |
| masked | UnitFileState | /etc/systemd/system/UNIT → /dev/null; no start until unmask |
Commands
systemctl — lifecycle
Definition: Start, stop, restart, reload, or query a systemd service unit.
| Option / verb | Argument | Meaning | Example |
|---|---|---|---|
status | UNIT | State, PID, recent logs snippet | systemctl status nginx |
status -l | UNIT | Full (untruncated) lines | systemctl status -l nginx |
start | UNIT | Start now | sudo systemctl start nginx |
stop | UNIT | Stop now | sudo systemctl stop nginx |
restart | UNIT | Stop then start; starts it if it was down | sudo systemctl restart nginx |
reload | UNIT | Reload app config (ExecReload=). Not the unit file | sudo systemctl reload nginx |
try-restart | UNIT | Restart only if already running | sudo systemctl try-restart nginx |
reload-or-restart | UNIT | Reload if ExecReload= exists, else restart; starts if down | sudo systemctl reload-or-restart nginx |
kill | UNIT | Signal the unit’s processes (default cgroup/all, not only MainPID) | sudo systemctl kill nginx |
kill -s | SIG UNIT | Specific signal. Main only: --kill-whom=main | sudo systemctl kill -s HUP nginx |
Flag combos
| Combo | Meaning | Example |
|---|---|---|
systemctl status UNIT --no-pager -l | Pasteable full status | Tickets / CI |
systemctl restart UNIT && systemctl is-active UNIT | Restart + verify |
systemctl — boot enablement
Definition: Control whether a unit starts at boot (enable/disable/mask).
| Option / verb | Argument | Meaning | Example |
|---|---|---|---|
enable | UNIT | Create [Install] symlinks (boot hook). Also daemon-reload. Needs an [Install] section | sudo systemctl enable nginx |
disable | UNIT | Remove those symlinks (and manual aliases). Does not stop unless --now | sudo systemctl disable nginx |
enable --now | UNIT | Enable + start now | sudo systemctl enable --now nginx |
disable --now | UNIT | Disable + stop now | sudo systemctl disable --now nginx |
is-enabled | UNIT | Prints enabled / disabled / masked / static / indirect / … Exit 0 for enabled and static | systemctl is-enabled nginx |
is-active | UNIT | Prints ActiveState (active / inactive / failed / activating…). Exit 0 only if active (includes exited) | systemctl is-active nginx |
is-failed | UNIT | Exit 0 if that unit failed. No args: any failed unit | systemctl is-failed nginx |
mask | UNIT | Prevent all starts | sudo systemctl mask nginx |
unmask | UNIT | Undo mask | sudo systemctl unmask nginx |
reenable | UNIT | Disable then enable (refresh links) | sudo systemctl reenable nginx |
Flag combos
| Combo | Meaning | Example |
|---|---|---|
enable --now | Boot + immediate | New service deploy |
mask then fix then unmask + start | Stop restart storms | Noisy crash loop |
systemctl — inspect & edit
Definition: Show unit files, properties, and create drop-in overrides.
| Option / verb | Argument | Meaning | Example |
|---|---|---|---|
cat | UNIT | Concatenate unit file + drop-ins from disk (stale if you skipped daemon-reload) | systemctl cat nginx |
show | UNIT | Live properties (key=value) | systemctl show nginx |
show -p | PROP UNIT | One property (comma-list OK) | systemctl show -p MainPID nginx |
list-units | — | Units in memory. Default: active, jobs, or failed — not every installed file | systemctl list-units --type=service |
list-unit-files | — | Installed files + enablement (enabled/disabled/static/…) | systemctl list-unit-files nginx* |
list-dependencies | UNIT | Requires= / Wants= tree (loaded units; targets expanded by default) | systemctl list-dependencies nginx |
edit | UNIT | Drop-in /etc/systemd/system/UNIT.d/override.conf + auto daemon-reload | sudo systemctl edit nginx |
edit --full | UNIT | Full replacement under /etc (masks vendor file) + auto reload | sudo systemctl edit --full nginx |
daemon-reload | — | Reload systemd’s unit files — not the daemon’s app config | sudo systemctl daemon-reload |
reset-failed | UNIT | Clear failed + start-limit + NRestarts. Does not start the unit | sudo systemctl reset-failed nginx |
daemon-reexec | — | Reexec systemd (rare) | Maintenance |
Flag combos
| Combo | Meaning | Example |
|---|---|---|
show -p MainPID,NRestarts,ActiveState UNIT | PID + restart count | Crash loop detect |
cat UNIT after edit | Confirm drop-in merged | |
daemon-reload && restart UNIT | After a manual unit/drop-in edit | systemctl edit already reloads |
Useful show properties
Definition: Key
systemctl showfields (MainPID, NRestarts, ActiveState, paths, deps).
| Property | Meaning |
|---|---|
MainPID | Main process ID |
NRestarts | Restart counter (crash loop clue) |
ExecMainStatus | Exit status of main process |
FragmentPath | Path to unit file |
DropInPaths | Drop-in files |
ActiveState / SubState | High-level / detailed state |
UnitFileState | enabled/disabled/masked |
MemoryCurrent | cgroup memory if accounting on |
Requires / Wants / After | Dependencies |
systemctl show nginx -p MainPID -p NRestarts -p ActiveState -p FragmentPath --no-pager
systemctl — global / output
Definition: List units, filter types, user vs system bus, non-pager output.
| Option | Argument | Meaning | Example |
|---|---|---|---|
--user | — | User session manager (not system) | systemctl --user status app |
--no-pager | — | No less; good for scripts | systemctl status u --no-pager |
--no-legend | — | Hide headers/legends | systemctl list-units --no-legend |
-q / --quiet | — | Suppress output; use exit code | systemctl is-active -q nginx |
--failed | — | List failed units | systemctl --failed |
-t / --type | TYPE | Filter type (service, timer) | systemctl -t service |
-a / --all | — | All units including inactive | systemctl list-units -a |
Flag combos
| Combo | Meaning | Example |
|---|---|---|
systemctl --failed --no-pager | What’s broken now | Morning check |
systemctl --user enable --now lab.service | User service | Lab without root |
Common recipes
| Goal | Command |
|---|---|
| Health snapshot | systemctl status UNIT -l --no-pager |
| Start + boot | sudo systemctl enable --now UNIT |
| Restart & check | sudo systemctl restart UNIT; systemctl is-active UNIT |
| See effective unit | systemctl cat UNIT |
| After a manual unit edit | sudo systemctl daemon-reload && sudo systemctl restart UNIT |
| Clear failed | sudo systemctl reset-failed UNIT |
| All failed | systemctl --failed |
| Main PID | systemctl show -p MainPID --value UNIT |
Pitfalls
enable≠start(boot hook vs now).enabledoes nothing useful without[Install](static).disabledcan still come up via socket, timer, D-Bus, or another unit’sWants=/Requires=.activeis not always a running process —active (exited)oneshots still makeis-activesucceed.- Vendor units live under
/usr/lib/systemd/system(often also/lib/…). Package upgrades overwrite those. Put overrides in/etcviasystemctl edit. systemctl editalready runsdaemon-reload. You stillrestart(orreload) for most process settings. Manualviof a unit file does needdaemon-reload.reloadis the application config (ExecReload=).daemon-reloadis systemd’s unit files. Do not mix them.reloadfails if there is noExecReload=— userestartorreload-or-restart.systemctl killis notkill $(MainPID)— it signals the cgroup by default.maskblocks every activation (manual and dependency). Rememberunmask. Local units already in/etcoften cannot be masked that way.- User services need
systemctl --userand oftenloginctl enable-lingerto start at boot without a login.
For more details, try man <command> in your terminal.