systemd
Table of Contents
1. Units
User units are stored in ~/.config/systemd/user/ and once activated
they start when the user login so the network connection should be
ready
1.1. Conditions
~$ systemd-analyze condition 'ConditionPathExists=%h/documents' test.service: ConditionPathExists=/home/alan/documents succeeded. Conditions succeeded. ~$ systemd-analyze condition 'ConditionHost=!foo' test.service: ConditionHost=!foo succeeded. Conditions succeeded.
1.2. Service units
1.2.1. Example
Starting SSH tunneling after the network stack has been started
Services using the network should hence simply place an
After=network.targetstanza in their unit files, withoutWants=network.targetorRequires=network.target.
[Unit] Description=Start the SSH tunnel After=network.target ConditionPathExists=%h/.ssh/config [Service] Type=simple ExecStart=/usr/bin/ssh tunnel Restart=always RestartSec=120 [Install] WantedBy=default.target
1.3. Timer units
- https://manpages.debian.org/stable/systemd/systemd.timer.5
- https://wiki.archlinux.org/title/Systemd/Timers
- Learn Linux TV on YouTube - Automate your tasks with systemd timers
- https://documentation.suse.com/smart/systems-management/html/systemd-working-with-timers/index.html
A timer unit foo.timer is used for timer-based activation of a matching service unit foo.service
1.3.1. List user timers
systemctl --user list-timers --all
1.3.2. Example
- Timer unit
Only the timer unit requires the
[Install]section in this case[Unit] Description=Backup fish history every sunday at 01:00 [Timer] OnCalendar=Sun 01:00 RandomizedDelaySec=5minutes Persistent=true Unit=backup-fish-history.service [Install] WantedBy=timers.target
- Service unit
The
%LETTERare specifiers, e.g.%hexpand to/home/alan[Unit] Description=Backup fish history After=network.target [Service] ExecStart=/usr/bin/rsync -avh %h/.local/share/fish/fish_history framboise:backup/%H/
- Enable the timer unit
systemctl --user enable --now backup-fish-history.timer
Check the logs with journalctl
1.3.3. Path units
[Unit] Description=Monitor foo.txt for changes [Path] PathChanged=%h/foo.txt Unit=foo.service
2. Scheduling
Use OnCalendar= directive for scheduling based on wall-clock time, as monotonic clock starts at boot time (e.g. using OnUnitActiveSec=1hour instead of OnCalendar=hourly)
To test the validity of the scheduled time
systemd-analyze calendar "Sun 01:00" --iteration=5
Will output
Original form: Sun 01:00
Normalized form: Sun *-*-* 01:00:00
Next elapse: Sun 2023-12-24 01:00:00 GMT
From now: 5 days left
Iter. #2: Sun 2023-12-31 01:00:00 GMT
From now: 1 week 5 days left
Iter. #3: Sun 2024-01-07 01:00:00 GMT
From now: 2 weeks 5 days left
Iter. #4: Sun 2024-01-14 01:00:00 GMT
From now: 3 weeks 5 days left
Iter. #5: Sun 2024-01-21 01:00:00 GMT
From now: 1 month 3 days left
3. Command to run after any changes to unit files
systemctl --user daemon-reload
4. systemd-tmpfiles
Creates, deletes, and cleans up volatile and temporary files and directories
- https://askubuntu.com/questions/88391/whats-an-uppercase-t-at-the-end-of-unix-permissions
- https://developers.redhat.com/blog/2016/09/20/managing-temporary-files-with-systemd-tmpfiles-on-rhel7
- https://manpages.debian.org/stable/systemd-standalone-tmpfiles/systemd-tmpfiles.8
- https://manpages.debian.org/stable/systemd-standalone-tmpfiles/tmpfiles.d.5
- https://www.redhat.com/sysadmin/suid-sgid-sticky-bit
See tmpfiles.d.html
5. Symlinks poweroff, shutdown and reboot to systemctl
The systemctl command knows when it has been invoked via symlink (argument $0 returns the executed command name)
~$ ls -l /usr/sbin/poweroff lrwxrwxrwx 1 root root 14 6 mars 15:56 /usr/sbin/poweroff -> /bin/systemctl ~$ cat foo #!/bin/sh echo "Invoked by $0" ~$ ln -sv foo bar 'bar' -> 'foo' ~$ ./foo Invoked by ./foo ~$ ./bar Invoked by ./bar