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. startandstopact now.enableanddisabledecide 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
$ 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
enablealone doesn't start the service now: add--now, or runstarttoo.- After editing a unit file, run
sudo systemctl daemon-reload, or systemd keeps using the old version. maskis stronger thandisable: a masked unit can't start at all, not even by hand or as a dependency.systemctl --usermanages your own units, separately from the system's, and without sudo.
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.