๐ Systemd#
Overview
See also
Apache Tomcat Multiple Instances with Systemd Template
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 source of one or more units.
systemctl cat pam_namespace.service
Show the properties of one or more units.
systemctl show 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
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 links.
.config/systemd
โโโ user
โโโ multi-user.target.wants
โย ย โโโ test.service -> /home/guigui/.config/systemd/user/test.service
โโโ test.service
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.
Template Unit
