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.target stanza in their unit files, without Wants=network.target or Requires=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

1.3.1. List user timers

systemctl --user list-timers --all

1.3.2. Example

  1. 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
    
  2. Service unit

    The %LETTER are specifiers, e.g. %h expand 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/
    
  3. 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

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

6. Manpages

7. Related nodes

8. References