Skip to content

systemd

October 31, 2023
January 5, 2017

systemd - Wikiwand
systemd - freedesktop.org
systemd - ArchWiki
systemd - Debian Wiki
TipsAndTricks
systemd.index

Rethinking PID 1
The Biggest Myths by author of systemd
EWONTFIX - Broken by design: systemd

Meet systemd, the controversial project taking over a Linux distro near you | PCWorld
How To Use Systemctl to Manage Systemd Services and Units | DigitalOcean
Systemd Essentials: Working with Services, Units, and the Journal | DigitalOcean
media.ccc.de - systemd in 2018

Archlinux, systemd-free

Unit files

Unit files are compiled to C code for faster bootup time.
man 5 systemd.unit, systemctl cat <unit>
systemctl list-unit-files - List all services in system

System services are written in unit files, located in:

Drop-in unit file:
/etc/systemd/system/httpd.service.d/myconf.conf
systemctl edit --full <service.unit>

This is what systemctl enable teamviewerd does:

ln -s '/usr/lib/systemd/system/teamviewerd.service' '/etc/systemd/system/multi-user.target.wants/teamviewerd.service'

Service units

Running Node.js on Linux with systemd - via @codeship | via @codeship
How to Run a Linux Program at Startup with systemd

!! Remember to systemctl enable <service> after adding service file. !!

therootcompany/serviceman: Cross-platform service management for Mac, Linux, and Windows.
Serviceman | webinstall.dev

sudo env PATH="$PATH" \
    serviceman add --system --username $(whoami) --name caddy -- \
        caddy run --config ./Caddyfile

Docker service example:

[Unit]
Description=Redis container
Requires=docker.service
After=docker.service

[Service]
Restart=always
ExecStart=/usr/bin/docker start -a redis_server
ExecStop=/usr/bin/docker stop -t 2 redis_server

[Install]
WantedBy=local.target
# /etc/systemd/system/docker-compose-gogs.service

[Unit]
Description=Gogs Service (Docker Compose)
Requires=docker.service
After=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/git/gogs
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=10

[Install]
WantedBy=multi-user.target

Timer units

man 5 systemd.timer
man 7 systemd.time
systemctl list-timers list all timers in system
systemd-run --on-active=1m <cmd> one shot timer

# timer.timer
[Unit]
Description=Timer to kick off timer service

[Timer]
# monotonic, every N seconds
OnBootSec=
OnUnitActiveSrc=

# realtime, akin to cron
OnCalendar=
Persistent=true # run on boot if last timer is missed

Unit= # unit name

[Install]
WantedBy=timer.target # run service of the same name by default

systemd container

systemd-nspawn

  1. place OS tree in /var/lib/machines/
machinectl pull-raw --verify=checksum <URL>
# set container root password
systemd-nspawn -D /var/lib/machines/<container name>
  1. systemd-nspawn -M <container name>
  2. machinectl enable <container name> or systemctl enable systemd-nspawn@<container name>

systemctl

systemctl command man page - systemd | ManKier

systemctl - List active services in system

systemctl --all - List all services in system

systemctl start SERVICE.service - Use it to start a service. Does not persist after reboot

systemctl stop SERVICE.service - Use it to stop a service. Does not persist after reboot

systemctl restart SERVICE.service - Use it to restart a service

systemctl reload SERVICE.service - If the service supports it, it will reload the config files related to it without interrupting any process that is using the service.

systemctl status SERVICE.service - Shows the status of a serviceTells whether a service is currently running.

systemctl enable SERVICE.service - Turns the service on, on the next reboot or on the next start event. It persists after reboot.

systemctl disable SERVICE.service - Turns the service off on the next reboot or on the next stop event. It persists after reboot.

systemctl is-enabled SERVICE.service - Check if a service is currently configured to start or not on the next reboot.

systemctl is-active SERVICE.service - Check if a service is currently active.

systemctl show SERVICE.service - Show all the information about the service.

systemctl daemon-reload: rewrite and reload all dependencies.

tools

systemd-cgls

systemd-ui

systemd-analyze command man page - systemd | ManKier analyse boot time

systemd-analyze blame | head -5

systemd specific locale and timezone:
localectl
timedatectl

hostnamectl: set-location

systemd-inhibit: run command with suspending

Lennart Poettering

Why systemd?
The Biggest Myths
systemd journal

systemd for Developers

systemd for Developers I
systemd for Developers II
systemd for Developers III

systemd for Administrators

TODO:
http://0pointer.net/blog/archives.html
select "systemd for Administrators" and generate links

systemd for Administrators, Part 1
systemd for Administrators, Part II
systemd for Administrators, Part III
....
systemd For Administrators, Part XXI

journalctl

journalctl command man page - systemd | ManKier
journalctl
journalctl: Query the systemd Journal | Reference | openSUSE Leap 42.3
Displaying Linux Log files with journalctl
Using journalctl -The Ultimate Guide to Logging
How To Use Journalctl to View and Manipulate Systemd Logs | DigitalOcean

# clean logs
journalctl --disk-usage
sudo journalctl --vacuum-size=500M
# jump to end
journalctl -e
# show only this boot
journalctl -b
# reverse logs
journalctl -r
# follow journal
journalctl -f
# time filtering `--since` and `--until`
journalctl --since "2019-09-25 15:00"
# filter to `docker` service
journalctl -u docker.service
# add entry to journal
echo "here" | systemd-cat

# kernel logs (akin to dmesg)
journalctl -k

# formatting output
# man 7 system.journal-fields
journalctl -o verbose
journalctl -o json-pretty

Binding

python-systemd package — python-systemd documentation
torfsen/python-systemd-tutorial: A tutorial for writing a systemd service in Python