Running Services with systemctl
systemctl is the remote control for systemd. It needs a running systemd (PID 1) and, for most actions, root, so every systemctl transcript in this lesson is a sample session from an Ubuntu 24.04 server. Try them on your practice VM.
The everyday verbs
| Command | Does | Survives reboot? |
|---|---|---|
systemctl start app / stop app |
start or stop now | no |
systemctl restart app |
stop, then start (drops connections) | no |
systemctl reload app |
run ExecReload= (re-read config, keep running) |
no |
systemctl reload-or-restart app |
reload if the unit supports it, else restart | no |
systemctl enable app / disable app |
create or remove the [Install] symlinks |
yes |
systemctl enable --now app |
enable and start in one step | yes |
systemctl mask app / unmask app |
link the unit to /dev/null: it can't be started at all, even as a dependency |
yes |
systemctl daemon-reload |
re-read all unit files after you edited them | – |
Two classic mistakes: editing a unit file and forgetting daemon-reload (systemd keeps running the old definition and warns about it in status), and start without enable (works until the next reboot).
Reading systemctl status
$ sudo systemctl enable --now inventory.service
Created symlink /etc/systemd/system/multi-user.target.wants/inventory.service → /etc/systemd/system/inventory.service.
$ systemctl status inventory.service
● inventory.service - Inventory API
Loaded: loaded (/etc/systemd/system/inventory.service; enabled; preset: enabled)
Active: active (running) since Thu 2026-01-15 10:00:02 UTC; 3min 12s ago
Main PID: 4182 (inventory)
Tasks: 6 (limit: 2275)
Memory: 18.4M (peak: 21.0M)
CPU: 412ms
CGroup: /system.slice/inventory.service
└─4182 /opt/inventory/bin/inventory serve
Jan 15 10:00:02 web1 systemd[1]: Starting inventory.service - Inventory API...
Jan 15 10:00:02 web1 systemd[1]: Started inventory.service - Inventory API.
Jan 15 10:00:02 web1 inventory[4182]: listening on :8000- Loaded: which file was loaded, whether it is enabled, and the vendor preset.
- Active: the state (
active (running),inactive (dead),failed,activating (auto-restart)) and since when. - CGroup: every process of the service. A process that "escaped" from a forking daemon still shows up here.
- The last lines come from the journal for this unit (Reading Logs with journalctl).
A failed service looks like this:
$ systemctl status inventory.service
× inventory.service - Inventory API
Loaded: loaded (/etc/systemd/system/inventory.service; enabled; preset: enabled)
Active: failed (Result: exit-code) since Thu 2026-01-15 10:04:40 UTC; 8s ago
Process: 4301 ExecStart=/opt/inventory/bin/inventory serve (code=exited, status=1/FAILURE)
Main PID: 4301 (code=exited, status=1/FAILURE)
CPU: 35ms
Jan 15 10:04:40 web1 inventory[4301]: error: open /etc/inventory/env: permission denied
Jan 15 10:04:40 web1 systemd[1]: inventory.service: Main process exited, code=exited, status=1/FAILURE
Jan 15 10:04:40 web1 systemd[1]: inventory.service: Failed with result 'exit-code'.
$ systemctl --failed
UNIT LOAD ACTIVE SUB DESCRIPTION
● inventory.service loaded failed failed Inventory API
1 loaded units listed.The Result: tells you how it died: exit-code, signal (killed, often by the OOM killer), timeout, core-dump, start-limit-hit. After fixing the cause, systemctl reset-failed inventory.service clears the state (and the start-limit counter).
systemctl in scripts
status is for humans. Scripts use the query verbs, which answer with an exit code and print one word:
$ systemctl is-active nginx; echo $?
active
0
$ systemctl is-enabled inventory; echo $?
enabled
0
$ systemctl is-failed inventory --quiet && echo "needs attention"
$ systemctl show inventory -p ActiveState -p SubState -p NRestarts -p MainPID
ActiveState=active
SubState=running
NRestarts=3
MainPID=4182systemctl show prints machine-readable Key=Value pairs for every property (there are hundreds; -p picks some). That format is easy to check from a monitoring script. Here is such a check, run against saved systemctl show output from three services:
SAMPLES = {
"nginx": "ActiveState=active\nSubState=running\nNRestarts=0\nResult=success",
"inventory": "ActiveState=active\nSubState=running\nNRestarts=14\nResult=success",
"backup": "ActiveState=failed\nSubState=failed\nNRestarts=0\nResult=exit-code",
}
def parse(text):
return dict(line.split("=", 1) for line in text.splitlines())
def check(name, props, max_restarts=5):
if props["ActiveState"] == "failed":
return f"CRIT {name}: failed ({props['Result']})"
if int(props["NRestarts"]) > max_restarts:
return f"WARN {name}: restarted {props['NRestarts']} times (flapping?)"
return f"OK {name}: {props['ActiveState']}/{props['SubState']}"
for name, text in SAMPLES.items():
print(check(name, parse(text)))OK nginx: active/running
WARN inventory: restarted 14 times (flapping?)
CRIT backup: failed (exit-code)
NRestarts is the counter people forget: a service that crashes and restarts every few minutes is always active when you look, but its users see errors.
Remote and user services
$ systemctl -H admin@web2 is-active nginx # over SSH, no shell needed on the other side
active
$ systemctl --user status syncthing # a service of your own user
$ sudo loginctl enable-linger deploy # let deploy's user services run without a login--user services run in the user's own systemd instance; with lingering enabled they start at boot, which is a simple way to run an app as an unprivileged user without root-owned unit files.
Test yourself
You edited /etc/systemd/system/inventory.service. What must you run before systemctl restart inventory?
- systemctl enable inventory
- systemctl daemon-reload
- systemctl reset-failed
- Nothing, systemd watches the file
What is the difference between disable and mask?
- None
disableremoves the boot-time symlinks but the unit can still be started (manually or as a dependency);maskmakes it impossible to startmaskdeletes the unit filedisablealso stops the service
Which command is best inside a health-check script?
- systemctl status app | grep running
- systemctl is-active --quiet app
- ps aux | grep app
- journalctl -u app