systemd Systemd

Overview

Systemd is a suite of basic building blocks for a Linux system.
It provides a system and service manager that runs as PID 1 and starts the rest of the system.
See the full documentation on the project Home Page.

systemctl

systemctl --help | grep -w "^  \(\(re\)*start\|stop\|cat\|show \)"
  start UNIT...                       Start (activate) one or more units
  stop UNIT...                        Stop (deactivate) one or more units
  restart UNIT...                     Start or restart one or more units
  show [PATTERN...|JOB...]            Show properties of one or more
  cat PATTERN...                      Show files and drop-ins of specified units

Show the runtime status.

systemctl status

Show the runtime status of one or more units.

systemctl status sshd.service pam_namespace.service

Show the runtime status of one or more patterns.

systemctl status "*mount"  "*.target"

List the units currently in memory.

systemctl list-units
systemctl list-units --failed
systemctl list-units --state=running
systemctl list-units -t target
systemctl list-units -t mount

List the socket units currently in memory.

systemctl list-sockets
systemctl list-sockets --show-types
systemctl list-sockets cups.socket systemd-rfkill.socket

List the timer units currently in memory.

systemctl list-timers
systemctl list-timers unbound-anchor.timer systemd-tmpfiles-clean.timer

Get the name of the default target.

systemctl get-default

systemd-analyze

systemd-analyze
systemd-analyze blame
systemd-analyze security
systemd-analyze security docker.service
systemd-analyze plot > systemd-analyze_plot.svg
systemd-analyze dot 'docker.*' | dot -Tsvg > docker.svg
  Color legend: black     = Requires
                dark blue = Requisite
                dark grey = Wants
                red       = Conflicts
                green     = After

docker.svg

../_images/docker.svg

User Unit

Create a unit file, ~/.config/systemd/user/test.service.

[Unit]
Description=%u test service
After=multi-user.target
AssertPathExists=%h

[Service]
WorkingDirectory=%h
EnvironmentFile=%h/.local/test.env
UMask=0002
ExecStart=/bin/sh -c 'while true; do echo -e "[ $(date) ] $PROJECT: %n is running on %H"; sleep 15; done'
ExecStop=/bin/kill -TERM $MAINPID
ExecStopPost=/bin/sh -c 'echo -e "[ $(date) ] $PROJECT: %n is stopped on %H"'
Restart=on-failure
Type=simple

[Install]
WantedBy=multi-user.target

Caution

Don’t use User and Group option in a user service unit.

Create an env file, ~/.local/test.env.

PROJECT=Guisam

Enable the new service unit.

systemctl --user enable test.service

Note

Enabling service create systemd link, LLL 1.

.config/systemd
└── user
    ├── multi-user.target.wants
    │   └── test.service -> /home/guigui/.config/systemd/user/test.service
    └── test.service
1

LLL … Lennart like links laugh

Start, stop the service.

systemctl --user start test.service
systemctl --user stop test.service

Check the service status.

systemctl --user status test.service
  ● test.service - guigui test service
     Loaded: loaded (/home/guigui/.config/systemd/user/test.service; enabled; vendor preset: enabled)
     Active: inactive (dead)

  nov. 09 17:34:41 laptop.guisam.lan systemd[11727]: Stopping guigui test service...
  nov. 09 17:34:41 laptop.guisam.lan sh[29210]: [ Sat 09 Nov 2019 05:34:41 PM CET ] Guisam: test.service is stopped on laptop.guisam.lan
  nov. 09 17:34:41 laptop.guisam.lan systemd[11727]: test.service: Succeeded.
  nov. 09 17:34:41 laptop.guisam.lan systemd[11727]: Stopped guigui test service.
  nov. 09 18:17:10 laptop.guisam.lan systemd[11727]: Started guigui test service.
  nov. 09 18:17:10 laptop.guisam.lan sh[635]: [ Sat 09 Nov 2019 06:17:10 PM CET ] Guisam: test.service is running on laptop.guisam.lan
  nov. 09 18:17:24 laptop.guisam.lan systemd[11727]: Stopping guigui test service...
  nov. 09 18:17:24 laptop.guisam.lan sh[950]: [ Sat 09 Nov 2019 06:17:24 PM CET ] Guisam: test.service is stopped on laptop.guisam.lan
  nov. 09 18:17:24 laptop.guisam.lan systemd[11727]: test.service: Succeeded.
  nov. 09 18:17:24 laptop.guisam.lan systemd[11727]: Stopped guigui test service.

Display the service logs.

journalctl --user -u test.service

Enable a persistent session.

sudo loginctl enable-linger <user>

Warning

You need root acces to access enable-linger option. /var/lib/systemd/linger/<user> must exist.