systemd Explained: Start, Stop and Enable Services
You start a small web app on a new server from your terminal. It stops when you close the terminal, and after a reboot nothing runs at all. On most Linux servers, the fix is systemd. This lesson explains what systemd does, how a unit file describes a service, and how to start, stop, enable and troubleshoot one.
What systemd is and does
A service is a program that runs in the background with no terminal, such as a web server (lesson 10). On most Linux distributions, such as Debian, Ubuntu, Fedora and Red Hat Enterprise Linux, systemd starts services at boot, watches them and stops them at shutdown.
At boot, the kernel, the core of the operating system (lesson 2), starts the first process, with process ID (PID) 1. On these systems that process is systemd, and it starts every service.
systemd works from units, small text files that each describe one thing it manages. Service units end in .service, such as cron.service. A target is a unit that groups others into a stage of boot; servers normally boot to multi-user.target, the normal state without a desktop.
In the flowchart, notice the footer: a failed service comes back only if its unit asks.
Unit files: a service's instruction card
You describe your own service in a unit file in /etc/systemd/system/. Packages install theirs in /usr/lib/systemd/system/ (shown as /lib/systemd/system/ on Debian 12), and a file in /etc with the same name wins.
This unit, saved as /etc/systemd/system/myapp.service, runs Python's built-in web server. It serves the folder /srv, usually empty, on port 8080 of 127.0.0.1, an address only this machine can reach. A port is a number that picks which program gets incoming traffic (IP addresses and ports).
[Unit]
Description=Demo web app
After=network.target
[Service]
User=myapp
WorkingDirectory=/srv
ExecStart=/usr/bin/python3 -m http.server 8080 --bind 127.0.0.1
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
Square brackets start a section; every other line is a Key=value setting. In the diagram, notice [Install]: only enable reads it, never start.
ExecStart is the command, with the program's full path (which python3 prints it). User runs it as an ordinary account; without this line it would run as root, against the least-privilege habit from lesson 7. Restart=on-failure starts it again after RestartSec seconds if it exits with an error code or crashes.
The account must exist first. This creates a system account for a service, with no home folder and a shell that refuses logins, and prints nothing when it works:
sudo useradd --system --shell /usr/sbin/nologin myapp
Everyday systemctl commands
You give systemd orders with systemctl. Commands that change a service need sudo; status only reads. myapp is short for myapp.service.
startandstop: run it, or stop it, now. stop sends SIGTERM, then SIGKILL after a timeout, 90 seconds by default (lesson 10).restart: stop, then start.enableanddisable: start, or stop starting, at every boot.daemon-reload: reread the unit files after you create or edit one.status: the state, the main PID and the last 10 log lines.
In the diagram, notice the side note: systemd keeps units in memory, so after an edit it uses the old version until daemon-reload.
On a test machine, create the file with an editor such as nano, then reload, start and check:
sudo nano /etc/systemd/system/myapp.service
sudo systemctl daemon-reload
sudo systemctl start myapp
systemctl status myapp
daemon-reload and start print nothing when they work. On Debian 12, status shows this (trimmed; numbers are illustrative):
● myapp.service - Demo web app
Loaded: loaded (/etc/systemd/system/myapp.service; disabled; preset: enabled)
Active: active (running) since Wed 2026-12-02 10:15:42 UTC; 6s ago
Main PID: 2210 (python3)
CGroup: /system.slice/myapp.service
└─2210 /usr/bin/python3 -m http.server 8080 --bind 127.0.0.1
The green dot and active (running) mean it is up, and Main PID is its process ID in ps. CGroup lists the service's control group (cgroup), a kernel feature that keeps its processes together, so stopping it stops them all. Loaded names the file systemd read, then says disabled: it will not come back after a reboot.
One trap: with the default service type, start succeeds as soon as systemd launches the program, even a missing one, so always check status.
start versus enable: now versus at boot
start runs the service now and changes nothing about the next boot. enable changes the next boot and starts nothing now: it reads [Install] and creates a symbolic link, a shortcut (lesson 5), in the folder listing the units multi-user.target wants.
sudo systemctl enable myapp
Debian 12 and Ubuntu 24.04 print this line; newer systemd versions quote the paths:
Created symlink /etc/systemd/system/multi-user.target.wants/myapp.service → /etc/systemd/system/myapp.service.
The Loaded line in status now says enabled, so the service starts at every boot. enable --now also starts the service. disable removes the link but leaves the service running; disable --now also stops it.
When a service fails: reading systemctl status
Suppose ExecStart says /usr/bin/pyhton3, a typo. After daemon-reload and a restart, status shows this (trimmed; times are illustrative):
● myapp.service - Demo web app
Loaded: loaded (/etc/systemd/system/myapp.service; enabled; preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Wed 2026-12-02 10:20:16 UTC; 2s ago
Main PID: 2301 (code=exited, status=203/EXEC)
Active says the last run failed and Restart=on-failure will try again; without Restart=, it would say failed, next to a red ×. Main PID shows the exit status, the number a process leaves when it ends, plus a name. The log lines below give the reason: No such file or directory.
| Status | Meaning | Check |
|---|---|---|
203/EXEC | could not run the ExecStart program | the path; the file is executable |
217/USER | the User= account is missing | create it with useradd |
200/CHDIR | could not enter WorkingDirectory= | the folder exists |
1/FAILURE | the program exited with an error | its own log lines |
Permission problems show up in two ways: 203/EXEC with Permission denied when the program file is not executable, or the program's own Permission denied when myapp cannot read a file it needs. Fix the owner or mode (lesson 6) rather than run the service as root.
A busy port is the other classic: normally only one program can listen on an address and port. If another holds 8080, python3 exits with 1/FAILURE and logs OSError: [Errno 98] Address already in use. To find it, ss lists sockets, the endpoints of network connections: -l listening ones, -t TCP, -n as numbers, -p with their process (sudo shows every user's).
sudo ss -ltnp | grep 8080
One line comes back (numbers are illustrative):
LISTEN 0 5 127.0.0.1:8080 0.0.0.0:* users:(("python3",pid=1987,fd=3))
An old python3, PID 1987, holds the port: stop it or pick another port, then restart myapp. No output means nothing listens on 8080. To remove the demo, run sudo systemctl disable --now myapp, delete the file and daemon-reload. journalctl, for a service's full log, comes in lesson 13.
Check yourself
Your new service runs, but after a reboot it is gone. What was missed, and why did start not cover it?
enable. start only runs the service now; enable links it into the units multi-user.target wants, which systemd starts at every boot.
You fix ExecStart and run sudo systemctl restart myapp, but status still shows the old path. Why?
systemd still uses its in-memory copy of the unit. Run sudo systemctl daemon-reload, then restart; status also warns that the unit file changed on disk.
A service with Restart=on-failure comes back with a new PID after kill -9. Why, and how do you stop it for good?
on-failure treats SIGKILL like a crash, so systemd starts it again. sudo systemctl stop is not undone, because you asked for it, and disable keeps it off after a reboot.