Systemd services question

Hello,

I think this is what was asked for? No idea how to format this correctly, so sorry…

System:
Kernel: 6.9.7-zen1-1-zen arch: x86_64 bits: 64 compiler: gcc v: 14.1.1
clocksource: tsc avail: acpi_pm
parameters: BOOT_IMAGE=/@/boot/vmlinuz-linux-zen
root=UUID=a647ace3-ddca-4e7d-9dc2-e18c06df28dd rw rootflags=subvol=@
loglevel=3 ibt=off
Desktop: KDE Plasma v: 6.1.2 tk: Qt v: N/A info: frameworks v: 6.3.0
wm: kwin_wayland vt: 1 dm: SDDM Distro: Garuda base: Arch Linux
Machine:
Type: Laptop System: TUXEDO product: TUXEDO InfinityBook S Gen8 v: N/A
serial: <superuser required> Chassis: type: 10 serial: <superuser required>
Mobo: NB01 model: NS5X_NS7XAU serial: <superuser required>
part-nu: IBSGEN8 uuid: <superuser required> UEFI: INSYDE v: 1.07.10RTR1
date: 03/08/2024
Battery:
ID-1: BAT0 charge: 73.9 Wh (100.0%) condition: 73.9/73.9 Wh (100.0%)
volts: 8.7 min: 7.7 model: Notebook BAT type: Li-ion serial: <filter>
status: full
CPU:
Info: model: 13th Gen Intel Core i7-1360P bits: 64 type: MST AMCP
arch: Raptor Lake level: v3 note: check built: 2022+ process: Intel 7 (10nm)
family: 6 model-id: 0xBA (186) stepping: 2 microcode: 0x4121
Topology: cpus: 1x cores: 12 mt: 4 tpc: 2 st: 8 threads: 16 smt: enabled
cache: L1: 1.1 MiB desc: d-8x32 KiB, 4x48 KiB; i-4x32 KiB, 8x64 KiB
L2: 9 MiB desc: 4x1.2 MiB, 2x2 MiB L3: 18 MiB desc: 1x18 MiB
Speed (MHz): avg: 457 high: 800 min/max: 400/5000:3700 scaling:
driver: intel_pstate governor: powersave cores: 1: 400 2: 400 3: 400 4: 400
5: 541 6: 400 7: 800 8: 400 9: 400 10: 400 11: 400 12: 784 13: 400 14: 400
15: 400 16: 400 bogomips: 83558
Flags: avx avx2 ht lm nx pae sse sse2 sse3 sse4_1 sse4_2 ssse3 vmx
Vulnerabilities: <filter>
Graphics:
Device-1: Intel Raptor Lake-P [Iris Xe Graphics] vendor: CLEVO/KAPOK
driver: i915 v: kernel alternate: xe arch: Gen-13 process: Intel 7 (10nm)
built: 2022+ ports: active: DP-5 off: eDP-1 empty: DP-1, DP-2, DP-3,
DP-4, DP-6, DP-7, HDMI-A-1 bus-ID: 00:02.0 chip-ID: 8086:a7a0
class-ID: 0300
Display: wayland server: X.org v: 1.21.1.13 with: Xwayland v: 24.1.0
compositor: kwin_wayland driver: X: loaded: modesetting
alternate: fbdev,intel,vesa dri: iris gpu: i915 display-ID: 0
Monitor-1: DP-5 res: 1920x1080 size: N/A modes: N/A
API: EGL v: 1.5 hw: drv: intel iris platforms: device: 0 drv: iris
device: 1 drv: swrast surfaceless: drv: iris wayland: drv: iris x11:
drv: iris inactive: gbm
API: OpenGL v: 4.6 compat-v: 4.5 vendor: intel mesa v: 24.1.3-arch1.1
glx-v: 1.4 direct-render: yes renderer: Mesa Intel Graphics (RPL-P)
device-ID: 8086:a7a0 memory: 15.16 GiB unified: yes display-ID: :1.0
API: Vulkan v: 1.3.279 layers: 3 device: 0 type: integrated-gpu
name: Intel Graphics (RPL-P) driver: mesa intel v: 24.1.3-arch1.1
device-ID: 8086:a7a0 surfaces: xcb,xlib,wayland device: 1 type: cpu
name: llvmpipe (LLVM 18.1.8 256 bits) driver: mesa llvmpipe
v: 24.1.3-arch1.1 (LLVM 18.1.8) device-ID: 10005:0000
surfaces: xcb,xlib,wayland
Audio:
Device-1: Intel Raptor Lake-P/U/H cAVS vendor: CLEVO/KAPOK
driver: snd_hda_intel v: kernel alternate: snd_soc_avs,snd_sof_pci_intel_tgl
bus-ID: 00:1f.3 chip-ID: 8086:51ca class-ID: 0403
Device-2: HP USB Audio driver: hid-generic,snd-usb-audio,usbhid type: USB
rev: 2.0 speed: 480 Mb/s lanes: 1 mode: 2.0 bus-ID: 3-6.2:5
chip-ID: 03f0:056b class-ID: 0300 serial: <filter>
API: ALSA v: k6.9.7-zen1-1-zen status: kernel-api tools: N/A
Server-1: PipeWire v: 1.2.0 status: active with: 1: pipewire-pulse
status: active 2: wireplumber status: active 3: pipewire-alsa type: plugin
4: pw-jack type: plugin tools: pactl,pw-cat,pw-cli,wpctl
Network:
Device-1: Intel Raptor Lake PCH CNVi WiFi driver: iwlwifi v: kernel
bus-ID: 00:14.3 chip-ID: 8086:51f1 class-ID: 0280
IF: wlp0s20f3 state: down mac: <filter>
Device-2: Realtek RTL8111/8168/8211/8411 PCI Express Gigabit Ethernet
vendor: CLEVO/KAPOK driver: r8169 v: kernel pcie: gen: 1 speed: 2.5 GT/s
lanes: 1 port: 3000 bus-ID: 2d:00.0 chip-ID: 10ec:8168 class-ID: 0200
IF: enp45s0 state: down mac: <filter>
Device-3: Realtek RTL8153 Gigabit Ethernet Adapter driver: r8152 type: USB
rev: 3.0 speed: 5 Gb/s lanes: 1 mode: 3.2 gen-1x1 bus-ID: 2-1.4:5
chip-ID: 0bda:8153 class-ID: 0000 serial: <filter>
IF: enp0s13f0u1u4 state: up speed: 1000 Mbps duplex: full mac: <filter>
IF-ID-1: tun0 state: unknown speed: 10000 Mbps duplex: full mac: N/A
Info: services: NetworkManager, systemd-timesyncd, wpa_supplicant
Bluetooth:
Device-1: Intel AX211 Bluetooth driver: btusb v: 0.8 type: USB rev: 2.0
speed: 12 Mb/s lanes: 1 mode: 1.1 bus-ID: 3-10:3 chip-ID: 8087:0033
class-ID: e001
Report: btmgmt ID: hci0 rfk-id: 0 state: down bt-service: enabled,running
rfk-block: hardware: no software: no address: <filter> bt-v: 5.3 lmp-v: 12
status: discoverable: no pairing: no
Drives:
Local Storage: total: 2.73 TiB used: 1.27 TiB (46.6%)
SMART Message: Unable to run smartctl. Root privileges required.
ID-1: /dev/nvme0n1 maj-min: 259:0 vendor: Samsung model: SSD 980 PRO 1TB
size: 931.51 GiB block-size: physical: 512 B logical: 512 B speed: 63.2 Gb/s
lanes: 4 tech: SSD serial: <filter> fw-rev: 5B2QGXA7 temp: 32.9 C
scheme: GPT
ID-2: /dev/sda maj-min: 8:0 vendor: Samsung model: PSSD T7
size: 931.51 GiB block-size: physical: 512 B logical: 512 B type: USB
rev: 3.2 spd: 5 Gb/s lanes: 1 mode: 3.2 gen-1x1 tech: SSD serial: <filter>
scheme: GPT
ID-3: /dev/sdb maj-min: 8:16 vendor: Samsung model: PSSD T7
size: 931.51 GiB block-size: physical: 512 B logical: 512 B type: USB
rev: 3.2 spd: 5 Gb/s lanes: 1 mode: 3.2 gen-1x1 tech: SSD serial: <filter>
scheme: MBR
Partition:
ID-1: / raw-size: 931.22 GiB size: 931.22 GiB (100.00%)
used: 30.47 GiB (3.3%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
ID-2: /boot/efi raw-size: 300 MiB size: 299.4 MiB (99.80%)
used: 584 KiB (0.2%) fs: vfat dev: /dev/nvme0n1p1 maj-min: 259:1
ID-3: /home raw-size: 931.22 GiB size: 931.22 GiB (100.00%)
used: 30.47 GiB (3.3%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
ID-4: /var/log raw-size: 931.22 GiB size: 931.22 GiB (100.00%)
used: 30.47 GiB (3.3%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
ID-5: /var/tmp raw-size: 931.22 GiB size: 931.22 GiB (100.00%)
used: 30.47 GiB (3.3%) fs: btrfs dev: /dev/nvme0n1p2 maj-min: 259:2
Swap:
Kernel: swappiness: 133 (default 60) cache-pressure: 100 (default) zswap: no
ID-1: swap-1 type: zram size: 31.05 GiB used: 0 KiB (0.0%) priority: 100
comp: zstd avail: lzo,lzo-rle,lz4,lz4hc,842 max-streams: 16 dev: /dev/zram0
Sensors:
System Temperatures: cpu: 44.0 C mobo: N/A
Fan Speeds (rpm): N/A
Info:
Memory: total: 32 GiB note: est. available: 31.05 GiB used: 4.29 GiB (13.8%)
Processes: 343 Power: uptime: 2h 40m states: freeze,mem,disk
suspend: s2idle avail: deep wakeups: 0 hibernate: platform avail: shutdown,
reboot, suspend, test_resume image: 12.41 GiB services: org_kde_powerdevil,
power-profiles-daemon, upowerd Init: systemd v: 256 default: graphical
tool: systemctl
Packages: pm: pacman pkgs: 1506 libs: 432 tools: octopi,pamac,paru
pm: flatpak pkgs: 0 Compilers: gcc: 14.1.1 Shell: garuda-inxi default: fish
v: 3.7.1 running-in: konsole inxi: 3.3.35
Garuda (2.6.26-1):
System install date:     2024-06-24
Last full system update: 2024-07-21
Is partially upgraded:   No
Relevant software:       snapper NetworkManager dracut
Windows dual boot:       No/Undetected
Failed units:

Anyway, I have 2 external SSD drives, so call then sda1 and sda2.
They are auto-mounted at start-up as /run/media//BACKUP and /run/media//EXTRA.

My systemd user service for Onedrive is perfectly happy with this, and starts syncing on log-in, cloning itself to /run/media//BACKUP/Onedrive, so we know the drives are connected correctly.

I have another system level service derived from the work here: Using Rsync to efficiently backup (mirror) hard drives // francium
which is intended to clone sda1 to sda2 every so often.

When I do:
systemctl enable drive-mirroring.timer

it creates the link, so good.

Then I do:
systemctl start drive-mirroring.timer

and nothing happens.

If I now do this:
systemctl start drive-mirroring.service

then the service starts and will from now on run when triggered by the timer (in my case hourly) until the system shuts down.

But on a reboot, I have to manually start the damned thing every time. So starting the timer is not starting the service, perhaps?

So, what am I doing wrong, please!?

Paste drive-mirroring.timer, drive-mirroring.service and the output of systemctl status drive-mirroring.timer (after a reboot) into the thread so we can take a look.

2 Likes

That is a wrong assumption. When a systemd unit is enabled, it means that it will start automatically on reboot (no need to start it).

Take some time reading man systemd.timer, and ask any question, if you cannot understand.

It follows the directives you assign to the timer unit, on when to start the service.
It does what it is told to do :man_shrugging: .

1 Like

Okay - good feedback, thanks… better than the snarky “RTFM” comment from the overlords - I think you can take as read that I did that, and I’m not a stupid person, OK?

So, my contents of “/etc/systemd/systemdrive-mirroring.timer” are:

#-----
[Unit]
Description=Drive sync
[Timer]
OnCalendar=hourly
Persistent=true
[Install]
WantedBy=timers.target
#-----

My contents of /etc/systemd/system/drive-mirroring.service are:

#-----
[Unit]
Description=Drive sync
[Service]
Type=simple
ExecStart=/usr/local/bin/drive-mirroring
#-----

And finally, my contents of /usr/local/bin/drive-mirroring are:

#-----
#!/bin/bash
echo "########### Drive mirroring starting at `date` ###########"
SOURCE=/run/media/keith/BACKUP/
DEST=/run/media/keith/EXTRA
rsync -ahvAE --delete --stats $SOURCE $DEST 2>&1
echo "########### Drive mirroring completed at `date` ###########"
#-----

OK, I have already done this:

systemctl enable drive-mirroring.timer

Then I did this:

systemctl start drive-mirroring.timer

After that, on a clean boot, if I do this:

systemctl status drive-mirroring.timer

then I get this:

#-----
● drive-mirroring.timer - Drive sync
Loaded: loaded (/etc/systemd/system/drive-mirroring.timer; enabled; preset: disabled)
Active: active (waiting) since Tue 2024-07-23 20:28:50 BST; 16min ago
Invocation: 3e4dc8a329dc471fb6bf0b879e32c840
Trigger: Tue 2024-07-23 21:00:00 BST; 15min left
Triggers: ● drive-mirroring.service
#-----

Also, if I do this:

journalctl -u drive-mirroring.service -f

I get this:

#-----
Jul 23 20:28:50 Tuxedo systemd[1]: Started Drive sync.
Jul 23 20:28:50 Tuxedo drive-mirroring[1272]: ########### Drive mirroring starting at Tue 23 Jul 20:28:50 BST
2024 ###########
Jul 23 20:28:50 Tuxedo drive-mirroring[1278]: sending incremental file list
Jul 23 20:28:50 Tuxedo drive-mirroring[1278]: rsync: [sender] change_dir "/run/media/keith/BACKUP" failed: No
such file or directory (2)
Jul 23 20:28:50 Tuxedo drive-mirroring[1282]: rsync: [Receiver] change_dir#3 "/run/media/keith" failed: No suc
h file or directory (2)
Jul 23 20:28:50 Tuxedo drive-mirroring[1282]: rsync error: errors selecting input/output files, dirs (code 3)
at main.c(829) [Receiver=3.3.0]
Jul 23 20:28:50 Tuxedo drive-mirroring[1272]: ########### Drive mirroring completed at Tue 23 Jul 20:28:50 BST
2024 ###########
Jul 23 20:28:50 Tuxedo systemd[1]: drive-mirroring.service: Deactivated successfully.
#-----

But, if I now do this:

#-----
Jul 23 20:48:20 Tuxedo systemd[1]: Started Drive sync.
Jul 23 20:48:20 Tuxedo drive-mirroring[4745]: ########### Drive mirroring starting at Tue 23 Jul 20:48:20 BST 2024 ###########
Jul 23 20:48:20 Tuxedo drive-mirroring[4762]: sending incremental file list
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: deleting OneDrive/Videos/Terry Prachett/Truckers/Terry PratchettTruckers 1992 Full Movie.mp4
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: OneDrive/Videos/Terry Prachett/Truckers/
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Number of files: 49,744 (reg: 48,007, dir: 1,737)
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Number of created files: 0
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Number of deleted files: 1 (reg: 1)
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Number of regular files transferred: 0
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Total file size: 681.78G bytes
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Total transferred file size: 0 bytes
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Literal data: 0 bytes
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Matched data: 0 bytes
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: File list size: 196.58K
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: File list generation time: 0.001 seconds
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: File list transfer time: 0.000 seconds
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Total bytes sent: 1.31M
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: Total bytes received: 2.05K
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: sent 1.31M bytes  received 2.05K bytes  138.32K bytes/sec
Jul 23 20:48:29 Tuxedo drive-mirroring[4762]: total size is 681.78G  speedup is 518,824.03
Jul 23 20:48:29 Tuxedo drive-mirroring[4745]: ########### Drive mirroring completed at Tue 23 Jul 20:48:29 BST2024 ###########
Jul 23 20:48:29 Tuxedo systemd[1]: drive-mirroring.service: Deactivated successfully.
#-----

So, manually starting the service seems to work. But ‘enabling’ the timer, does not. I truly do not understand, and would welcome help. But not the “RTFM” so-called help, OK everyone?!

Thanks for any helpful assistance.

Oh, and as an aside here, if we get this working, I want the mirroring to happen ever 10 mins, OK? But if I change this line:

OnCalendar=hourly

So using the stuff I have read in the man pages you might think:

OnCalendar=*:*/10

then I get an error when I try and ‘start’ the timer.

So, the whole thing is screwed ATM other than I can start the service manually which is not persistent over a reboot.

Most likely the drives are not mounted or accessible when the timer triggers the service. Later, when you start the service manually the drives are ready and the routine succeeds.

A couple easy things to try would be delaying the service start, or adding a dependency on the mount points, or adding a restart on failure directive.

Adding a delay can be as simple as having a ExecStartPre directive which runs the sleep command:

[Unit]
Description=Drive sync
After=local-fs.target

[Service]
Type=simple
ExecStartPre=/bin/sleep 30
ExecStart=/usr/local/bin/drive-mirroring

I put 30 seconds there but you can use more or less time than that as you see fit.

Adding a dependency for the mount point will probably also work. Something like this:

[Unit]
Description=Drive sync
After=local-fs.target
RequiresMountsFor=/run/media/keith/BACKUP
RequiresMountsFor=/run/media/keith/EXTRA

[Service]
Type=simple
ExecStart=/usr/local/bin/drive-mirroring

If you can get that working, that’s probably the cleanest solution in my opinion.

Restarting on failure will probably work too, although it’s a little hacky because it doesn’t actually fix the issue (it’s more of a workaround). You can just add it to the [Service] part and specify how long to wait before trying again after the service fails.

[Service]
Type=simple
ExecStart=/usr/local/bin/drive-mirroring
Restart=on-failure
RestartSec=30

After modifying the service file, reload the systemd configuration and start the timer to test:

sudo systemctl daemon-reload
sudo systemctl restart drive-mirroring.timer

This is not a valid format; the second wildcard should be a zero like this:

OnCalendar=*:0/10

This tells systemd to trigger the timer every 10 minutes, starting at the beginning of each hour.

3 Likes

The “RequiresMountsFor” seemed to do the trick, for which I thank you very much.

Edit: It seems like the timer correction works as well - I owe you a beer!

1 Like