systemd

journalctl
http://www.panticz.de/journalctl

Services

# list services
service  --status-all
systemctl list-units --type service --state running,failed
 
# enable service
systemctl enable service_name
sudo systemctl enable --now service_name
 
# disable service
systemctl disable service_name
 
systemctl status mariadb.service
 
journalctl -f
systemd-analyze verify <unit>
 
systemd-analyze critical-chain
systemctl list-units
systemctl list-unit-files --type=service
 
# override / edit job
systemctl edit cron.service
# systemd/system/cron.service.d/override.conf
 
# list reboots
journalctl --list-boots
 
# list dhcp client logs
sudo journalctl --since "7 days ago" -u dnsmasq-dhcp
sudo journalctl --since "7 days ago" | grep -i dhcp
 
# suspend system
sleep 10m && systemctl suspend
 
# Fix slow SSH login
systemctl restart systemd-logind
 
# restart user service
systemctl --user restart pulseaudio.service
 
# list all service names
systemctl list-units -t service --state active --no-pager --full --plain --no-legend "ceph*" | awk '/ceph-/ {print $1}'

Example

[Unit]
Description=preseed late_command script
After=network.target

[Service]
StandardOutput=tty
ExecStartPre=/usr/bin/wget -q --no-check-certificate https://raw.githubusercontent.com/panticz/preseed/master/late_command.sh -O /tmp/late_command.sh
ExecStart=/bin/bash /tmp/late_command.sh
ExecStartPost=/bin/systemctl disable late_command.service

[Install]
WantedBy=multi-user.target
>
# test
[Unit]
After=getty.target
 
[Service]
TTYPath=/dev/tty1
 
# test
journalctl -xn
systemctl list-unit-files --all

Disable systemd-resolved service

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
 
# disable systemd-resolve
sed -i 's/DNSStubListener=*/DNSStubListener=no/g' /etc/systemd/resolved.conf
service systemd-resolved stop
 
# /etc/NetworkManager/NetworkManager.conf:
[main]
dns=default
 
rm /etc/resolv.conf
sudo systemctl restart NetworkManager

Time synchronization / NTP
https://feeding.cloud.geek.nz/posts/time-synchronization-with-ntp-and-systemd/

apt install -y systemd-timesyncd
 
# cat /etc/systemd/timesyncd.conf
...
[Time]
NTP=ntp.ubuntu.com
FallbackNTP=ntp2.ubuntu.com
...
 
# apt purge ntp ntpdate
timedatectl set-ntp true
sudo systemctl restart systemd-timesyncd
timedatectl status
sudo journalctl -u systemd-timesyncd -f
 
# show contab log
sudo systemctl status crond

Timeserver / sync

sudo apt install -y tzdata
https://www.tecmint.com/set-time-timezone-and-synchronize-time-using-timedatectl-command/
# status
timedatectl  status
# disable
timedatectl set-ntp 0
# enable
timedatectl set-ntp true
timedatectl set-timezone "Europe/Berlin"
#timedatectl set-timezone "Europe/Zurich"
timedatectl set-time 14:14:30
timedatectl set-time 20181120
timedatectl set-time '18:00:00 2018-11-22'
timedatectl set-local-rtc 1
 
# sync time
systemctl restart systemd-timesyncd
timedatectl status

Status

systemctl is-active application.service
systemctl is-enabled application.service
systemctl is-failed application.service

Reload systemd configuration

systemctl daemon-reload
 
# list failed services
systemctl list-units --failed
journalctl -xe

Restart service on failure

...
[Service]
RestartSec=30s
Restart=on-failure

Restart another service (if running) on start

[Service]
...
ExecStartPost=/bin/systemctl try-restart wildfly.service
 
sudo systemctl edit apache2.service

systemd timers example with rsnapshot
http://mark.stosberg.com/blog/tech/rsnapshot-and-systemd/

systemctl cat flanneld.service

Extend service

systemctl edit sshd.service
# cat /etc/systemd/systemsshd.service.d/override.conf
[Service]
StandardOutput=tty
ExecStartPre=/bin/ping -c 30 bifrost

Debug stack

systemctl list-units | grep stack
journalctl -u devstack@q-agt.service

List names from running services

systemctl list-units --type=service --state=running | awk '/.*\.service/ {print $1}'
 
# stop running services
systemctl list-units --type=service --state=running | awk '/.*\.service/ {print $1}' | grep -Ev "systemd|unbound|ssh|dbus|getty|network|rsyslog|uuid" | xargs systemctl stop

Links
https://www.digitalocean.com/community/tutorials/how-to-use-journalctl-to-view-and-manipulate-systemd-logs
https://www.freedesktop.org/software/systemd/man/systemd.unit.html
https://wiki.ubuntuusers.de/systemd/
https://wiki.ubuntuusers.de/systemd/Service_Units/
http://www.freedesktop.org/software/systemd/man/systemd.service.html
https://wiki.archlinux.org/index.php/Systemd_FAQ
https://wiki.ubuntu.com/SystemdForUpstartUsers
http://www.freedesktop.org/software/systemd/man/systemd.special.html
https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services-and-units