Systemd Praxis Lösungen

Aus Xinux Wiki
Zur Navigation springen Zur Suche springen

Erstellen von Units

  • cat /etc/systemd/system/date1.service
[Unit]
Description=Simple Unit

[Service]
Type=simple
ExecStart=/usr/local/sbin/test-system.sh 1

[Install]
WantedBy=default.target
  • cat /etc/systemd/system/date2.service
[Unit]
Description=Simple Unit

[Service]
Type=simple
ExecStart=/usr/local/sbin/test-system.sh 2

[Install]
WantedBy=default.target
  • cat /etc/systemd/system/date3.service
[Unit]
Description=Simple Unit

[Service]
Type=simple
ExecStart=/usr/local/sbin/test-system.sh 3

[Install]
WantedBy=default.target
  • systemctl start date1
  • systemctl start date2
  • systemctl start date3
  • tail /tmp/test-system.log

Analyse der gestarteten Units

  • RemainAfterExit=yes hinterlässt den Service im activen Zustand
  • systemctl status date3
● date3.service - Simple Unit
Loaded: loaded (/etc/systemd/system/date3.service; disabled; vendor preset: enabled)
Active: active (exited) since Tue 2023-01-10 11:17:02 CET; 40min ago
Main PID: 11089 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 1129)
Memory: 0B
CPU: 0
CGroup: /system.slice/date3.service
    
Jan 10 11:17:02 debian-test systemd[1]: Starting Simple Unit...
Jan 10 11:17:02 debian-test systemd[1]: Finished Simple Unit.
  • systemctl status date1
● date1.service - Simple Unit
Loaded: loaded (/etc/systemd/system/date1.service; disabled; vendor preset: enabled)
Active: inactive (dead) since Tue 2023-01-10 12:02:16 CET; 46s ago
Process: 13868 ExecStart=/usr/local/sbin/test-system.sh 1 (code=exited, status=0/SUCCESS)
Main PID: 13868 (code=exited, status=0/SUCCESS)
CPU: 7ms

Jan 10 12:02:16 debian-test systemd[1]: Starting Simple Unit...
Jan 10 12:02:16 debian-test systemd[1]: date1.service: Succeeded.
Jan 10 12:02:16 debian-test systemd[1]: Finished Simple Unit.

Unit Abhängigkeiten definieren

  • systemctl enable date1
  • systemctl enable date2
  • systemctl enable date3
  • reboot
  • Die Skripte werden in einer zufälligen Reihenfolge gestartet
  • cat /etc/systemd/system/date1.service
[Unit]
Description=Simple Unit
Wants=date2.service
After=date2.service

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/test-system.sh 1

[Install]
WantedBy=default.target
  • cat /etc/systemd/system/date2.service
[Unit]
Description=Simple Unit
Wants=date3.service
After=date3.service

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/test-system.sh 2

[Install]
WantedBy=default.target
  • cat /etc/systemd/system/date3.service
[Unit]
Description=Simple Unit

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/test-system.sh 3

[Install]
WantedBy=default.target
  • tail -n 3 /tmp/test-system.log
test-system-3 Tue 10 Jan 2023 12:17:37 PM CET
test-system-2 Tue 10 Jan 2023 12:17:37 PM CET
test-system-1 Tue 10 Jan 2023 12:17:37 PM CET

Analyse des Bootvorgangs mit journalctl

  • journalctl -b
  • journalctl -b | grep “Network driver”
  • journalctl -b | grep usb
  • journalctl -b -u date1
  • journalctl -b -u date2
  • journalctl -b -u date3

Erstellen von Timer (Ersatz von cron und at)

Aufgabe 1

  • cat /etc/systemd/system/date1.timer
[Unit]
Description=Run date1.service every 2 minutes 

[Timer]
OnUnitActiveSec=2m
[Install]
WantedBy=timers.target
  • systemctl stop date1.service
  • systemctl start date1.timer
  • Wenn der Service noch aktiv ist kann es nicht durch den Timer getriggert werden

Aufgabe 2

  • cat /etc/systemd/system/get-octett.service
[Unit]
Description=Get Octett

[Service]
Type=simple
ExecStart=/usr/local/sbin/get-octett

[Install]
WantedBy=multi-user.target 
  • cat /etc/systemd/system/get-octett.timer
[Unit]
Description=Get Octett

[Timer]
OnBootSec=10s

[Install]
WantedBy=timers.target