Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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-failure means a crashed service comes back on its own.
  • Timers replace cron: *.timer units schedule work with the same reliability as the rest of systemd.
  • Logging is built in: journalctl captures 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
SuffixWhat It Is
.serviceA process to start and supervise.
.timerA scheduled job (like cron).
.mountA filesystem to mount.
.socketA socket to listen on, activating a service.
.pathWatches 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
  • status shows whether the unit is active, its recent logs, and its exit code.
  • enable vs. start: enable makes it boot-time; start makes 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-reload so 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; -f follows; -b shows 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.
  • enable does not start: a unit can be enabled (boot-time) but not currently running — check status.
  • Timers need a matching service: backup.timer only works if backup.service exists.
  • User units vs. system units: systemctl --user manages per-user units; most system services need plain systemctl.

PageWhy It Matters
psSee the processes a service actually started.
topWatch a service’s CPU and memory live.
killStop a misbehaving process directly.
nohupThe manual alternative to a service unit.

systemd Cheatsheet

CommandWhat It Does
systemctl start svcStart a service now.
systemctl stop svcStop a service.
systemctl restart svcRestart a service.
systemctl status svcShow state and recent logs.
systemctl enable svcStart at boot.
systemctl disable svcNever start at boot.
systemctl list-timersList scheduled timers.
systemctl cat svcShow the unit’s file.
systemctl daemon-reloadReload unit files after edits.
journalctl -u svc -fFollow a unit’s logs.