zibaldone:linux:systemd

Systemd

systemctl status

show running units

systemctl

show available unit files

systemctl list-unit-files

enable unit

systemctl enable name.service

start unit

systemctl start name

after change unit file

systemctl daemon-reload

enable systemd for specific user

loginctl enable-linger <username>
  • add user to systemd-journal group to permit journactl operations
usermod -a -G systemd-journal <user>
  • modify /etc/systemd/journald.conf
[Journal]
Storage=persistent

deprecated

/etc/profile.d/systemd-user.sh
export XDG_RUNTIME_DIR="/run/user/$UID"
export DBUS_SESSION_BUS_ADDRESS="unix:path=${XDG_RUNTIME_DIR}/bus"

new method

mkdir -p ~/.bashrc.d
echo "export XDG_RUNTIME_DIR=/run/user/$(id -u)" > ~/.bashrc.d/systemd 
source ~/.bashrc.d/systemd

journalctl -u does not work. Use instead

journalctl --user --user-unit

Una valida e più potente alternativa a cron, integrata in systemd, è rappresentata dai timers, di primo impatto soprattutto nell'aspetto supervisivo tramite journalctl, ma non solo (vedi risposta a questo quesito http://unix.stackexchange.com/questions/278564/cron-vs-systemd-timers).

Per attivare un timer servono essenzialmente due cose:

  • Il timer: /etc/systemd/system/nms-restart.timer
[Unit]
Description=Perform a nms resrtart

[Timer]
OnCalendar=hourly

[Install]
WantedBy=timers.target
  • il servizio: /etc/systemd/system/nms-restart.service
[Unit]
Description=Perform a nms restart

[Service]
Type=simple
Nice=19
IOSchedulingClass=2
IOSchedulingPriority=7
ExecStart=/usr/local/sbin/nms-restart

Poi si avvia e abilita il timer:

sudo systemctl start nms-restart.timer
sudo systemctl enable nms-restart.timer

Per la lista dei timer

systemctl list-timers --all

Per monitorare si usa journalctl in tutte le sue forme, per esempio:

journalctl -f -u nms-restart
  • zibaldone/linux/systemd.txt
  • Last modified: 2024/06/04 15:00
  • by sscipioni