systemd

systemd

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

services
# list services
service --status-all

# enable service
systemctl enable service_name

# disable service
systemctl disable service_name

systemctl status service_name
journalctl -f
systemd-analyze verify

# override / edit job
systemctl edit cron.service
# systemd/system/cron.service.d/override.conf

Example
<?php
$URL="https://raw.githubusercontent.com/panticz/preseed/master/late_command.service";
echo "wget -q --no-check-certificate $URL -O /etc/systemd/system/late_command.service && \
systemctl enable late_command.service";
echo "

";
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $URL);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
echo htmlspecialchars(curl_exec($c));
curl_close($c);
echo "

";
?>

# test
[Unit]
After=getty.target

[Service]
TTYPath=/dev/tty1

# test
journalctl -xn
systemctl list-unit-files --all

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

DNS / resolv
systemd-resolve --status

Flush DNS cache
sudo systemd-resolve --flush-caches

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

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/

Links
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