devutils/ATFQ/book ATFQ git

Part II · Your system

22.1 Units and timers

alsosystemdsystemctlunitunitsserviceservicestimertimerscroncrontab

TL;DR

  • systemd manages units: services (.service), timers (.timer), mounts, sockets, and targets, which group other units.
  • start and stop act now. enable and disable decide what starts at boot. They are separate.
  • A timer starts a service on a schedule: it is systemd's version of cron.
systemctl status nginx
sudo systemctl enable --now nginx   # start now, and at every boot
systemctl list-timers

On your machine

alice@laptopexample
$ atfq 22.1
  State     degraded: some units failed
  Failed    backup.service (Nightly backup)

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 2 questions

What is a target?

A target is a named group of units that marks a stage: multi-user.target is a working system without a graphical login, graphical.target adds one. At boot, systemd starts everything the default target needs (see 20.1). Targets replaced the numbered runlevels of the older SysV init.

Timers or cron?

Timers log every run in the journal, can run a job missed while the machine was off (Persistent=true), and their service can be started by hand to test it. A timer runs the service of the same name:

[Timer]
OnCalendar=*-*-* 03:00:00
Persistent=true

cron stays simpler for a one-line job, and is still available.

Commands
systemctl
Paths
/etc/systemd/system /usr/lib/systemd/system
See also
22.2 Why a service won't start
22.3 Unit files and overrides
20.1 From firmware to init