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
 
# 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

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

DNS

# Get DNS resolv status
systemd-resolve --status
 
# Flush DNS cache
sudo systemd-resolve --flush-caches

Disable systemd-resolved service

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved
 
# /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/

# 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-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

Cleanup logs / journal

journalctl --disk-usage
 
# Clear systemd journals older than X days
journalctl --vacuum-time=31d
 
# Clear systemd journals if they exceed X storage
journalctl --vacuum-size=1G

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