Systemd
A system and service manager for Linux. Runs as a daemon with PID 1.
Learn
Units
Units are systemd tasks.
The most common units are services (.service
files).
See man systemd.unit
for documentation.
Locations:
~/.config/systemd/user
: normal units/etc/systemd/system
: root units
Commands:
systemctl daemon-reload
: load new unit filessystemctl start/stop/restart <service>
: control a servicesystemctl enable/disable <service>
: run a service on bootsystemctl status <service>
: get the status of a service
Service file example:
[Unit]
Description=Run foo
## wait for network
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
Restart=always
Environment=FOO=BAR
EnvironmentFile=.env
ExecStart=/usr/local/bin/foo
[Install]
WantedBy=multi-user.target # run on boot
Targets (.target
files) are groups of units.
For example, graphical.target
contains all units that are necessary to run GUI.
At boot time, systemd activates default.target
.
Timers
https://wiki.archlinux.org/title/systemd/Timers
An alternative to cron jobs.
By convention, a timer unit starts a service unit with the same name, e.g. foo.timer
starts foo.service
.
Timer file example:
[Unit]
Description=Run foo weekly
[Timer]
OnCalendar=Weekly
Persistent=true
[Install]
WantedBy=timers.target
Logs
Use journalctl
to view systemd logs:
journalctl --unit foo.service