systemd Units Guide for Services, Timers, and the Tools That Manage Them
A systemd unit is any resource systemd manages — a service, a timer, a mount, a socket — and systemctl is the command that starts, stops, and watches them.
What is a systemd unit, and Why Learn It?
Most Linux systems today are run by systemd, the init system that starts everything at boot and keeps services alive. A unit is the basic object systemd manages: a service (*.service), a scheduled job (*.timer), a filesystem mount (*.mount), a network socket (*.socket), and more. Learning units is how you move from “I run this program by hand” to “this program runs at boot, restarts when it crashes, and logs its own problems” — the difference between a toy and a server.
Why Learn systemd Units?
- Services survive reboots: a unit starts automatically at boot; a hand-run command does not.
- Auto-restart on failure:
Restart=on-failuremeans a crashed service comes back on its own. - Timers replace cron:
*.timerunits schedule work with the same reliability as the rest of systemd. - Logging is built in:
journalctlcaptures every unit’s output — no log files to configure.
A systemd unit is a contract: you describe what the program should do, and systemd makes it happen — at boot, on failure, and on schedule.
1. The Unit Types
# List all units of a type
systemctl list-units --type=service
systemctl list-units --type=timer
systemctl list-units --type=mount
| Suffix | What It Is |
|---|---|
.service | A process to start and supervise. |
.timer | A scheduled job (like cron). |
.mount | A filesystem to mount. |
.socket | A socket to listen on, activating a service. |
.path | Watches a path and triggers a service. |
- The suffix tells systemd how to treat the unit; the name before it is yours to choose.
2. Managing Services with systemctl
# Start / stop / restart a service
sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl restart nginx
# Is it running?
systemctl status nginx
# Start at boot?
sudo systemctl enable nginx # on every boot
sudo systemctl disable nginx # never
statusshows whether the unit is active, its recent logs, and its exit code.enablevs.start:enablemakes it boot-time;startmakes it run now.
3. Timers: Scheduling with systemd
# List timers
systemctl list-timers
# A timer fires a matching service on a schedule
systemctl start backup.timer
A .timer unit is paired with a .service of the same name. The timer says when; the service says what. This is the systemd replacement for cron, with the same logging and reliability as everything else.
4. Reading Unit Files
# Show the file that defines a unit
systemctl cat nginx.service
# Show all properties of a unit
systemctl show nginx.service
# Reload systemd after editing a unit file
sudo systemctl daemon-reload
- Unit files live in
/etc/systemd/system/(yours) and/usr/lib/systemd/system/(from packages). - After editing any unit file, run
daemon-reloadso systemd notices the change.
5. Watching Logs with journalctl
# All logs for one unit
journalctl -u nginx
# Follow logs live (like tail -f)
journalctl -u nginx -f
# Logs since boot
journalctl -u nginx -b
-u <unit>filters to one unit;-ffollows;-bshows only this boot.- This is where a unit’s stdout, stderr, and systemd’s own messages all land.
Common Pitfalls
- Editing a unit file without
daemon-reload: systemd keeps using the old definition until you reload. enabledoes not start: a unit can be enabled (boot-time) but not currently running — checkstatus.- Timers need a matching service:
backup.timeronly works ifbackup.serviceexists. - User units vs. system units:
systemctl --usermanages per-user units; most system services need plainsystemctl.
Related Pages
| Page | Why It Matters |
|---|---|
ps | See the processes a service actually started. |
top | Watch a service’s CPU and memory live. |
kill | Stop a misbehaving process directly. |
nohup | The manual alternative to a service unit. |
systemd Cheatsheet
| Command | What It Does |
|---|---|
systemctl start svc | Start a service now. |
systemctl stop svc | Stop a service. |
systemctl restart svc | Restart a service. |
systemctl status svc | Show state and recent logs. |
systemctl enable svc | Start at boot. |
systemctl disable svc | Never start at boot. |
systemctl list-timers | List scheduled timers. |
systemctl cat svc | Show the unit’s file. |
systemctl daemon-reload | Reload unit files after edits. |
journalctl -u svc -f | Follow a unit’s logs. |