Systemd Summary

Systemd User mk units:

~/.config/systemd/user/*

Systemd Programs:
  • systemctl

  • loginctl

User Linger:

This means that a systemd user service is going to be started upon reboot Even if user is not logged in yet. This is enabled by loginctl enable-linger mk

Some useful snippets

systemctl --user list-units --type=target

systemctl list-unit-files

systemctl --user list-unit-files

sudo systemctl daemon-reload

systemctl --user start nexus
systemctl --user enable nexus
systemctl --user disable nexus

Systemd User Unit base Template:

# man pages in webpage
# https://man7.org/linux/man-pages/man5/systemd.service.5.html
# https://man7.org/linux/man-pages/man5/systemd.unit.5.html
# https://www.freedesktop.org/software/systemd/man/systemd.directives.html

[Unit]
Description=Redis Lastest (6.2.6) Server

After=network-online.target
# Before=

# weak dependency
Wants=network-online.target systemd-networkd-wait-online.service

# stronger dependency on other units
# Requires=

# will fail unless the listed unit(s) are already started
# can be conbined with 'After='
# Requisite=

[Service]
# https://www.freedesktop.org/software/systemd/man/systemd.service.html#Type=
# In general, most services will be good with simple
Type=simple

Environment="SHELL=/bin/bash" "BASH_ENV=/bits/_env.sh"

WorkingDirectory=/bits/data/redis

# This relies on the BASH_ENV environment file
ExecStart=/bin/bash -c "/zbin/redis/src/redis-server /bits/stargate/common/red/redis.conf"

# https://www.freedesktop.org/software/systemd/man/systemd.service.html#Restart=
# Restart=always
Restart=on-failure
RestartSec=10

[Install]
# this is primarily used when the service is "enabled" and "disabled" by
# systemctl --user enable service

# default target is a symlink to graphical.target on desktop
# or multi-user.target on a server
WantedBy=default.target

# WantedBy=graphical.target
# WantedBy=multi-user.target

Systemd Unit with auto recovery:

[Unit]
Description=Your Daemon
After=network-online.target
Wants=network-online.target systemd-networkd-wait-online.service

StartLimitIntervalSec=500
StartLimitBurst=5

[Service]
Restart=on-failure
RestartSec=5s

ExecStart=/path/to/daemon

[Install]
WantedBy=multi-user.target

Targets

achieved when the system reached certain states, or run-levels:

Run Lvl Target Units                        Description
0       runlevel0.target, poweroff.target   Shut down and power off
1       runlevel1.target, rescue.target     Set up a rescue shell
2,3,4   runlevel[234].target,               Set up a non-gfx multi-user shell
        multi-user.target
5       runlevel5.target, graphical.target  Set up a gfx multi-user shell
6       runlevel6.target, reboot.target     Shut down and reboot the system

The default.target is a sym link to one of the targets above. Use this to get the default

systemctl get-default

On a desktop, that would be the graphical.target On a server, that would be multi-user.target

https://www.landoflinux.com/linux_runlevels_systemd.html

  • network-online.target When the system have gone online (IPs established, DHCP, etc)

systemctl list-dependencies network-online.target
systemctl list-dependencies network.target
systemctl list-units | grep network