You are not logged in.

#1 2021-08-08 22:40:28

aetherh4cker
Member
Registered: 2015-05-12
Posts: 11

Systemd Timer Only When Wifi Is Connected?

Hey all!

So, I have a laptop that I want a daily backup script to run.  The script works great, but I'm wondering if there is a way to make it only run when WiFi is connected.

What happens is that I close my laptop lid to go to sleep.  When I open the lid, the backup timer kicks off and immediately fails because the WiFi takes a few seconds to connect.  I've semi-fixed this by adding in a "Restart on-failure"... so it will try again a few minutes later.  I was wondering if anyone knows a cleaner approach that will actually wait for an active wifi connection.

My laptop runs KDE  with NetworkManager... not sure if those provide any tools to help....

This is what I have right now:

backup.timer
[Unit]
Description=Daily Backup Timer

[Timer]
OnCalendar=daily

[Install]
WantedBy=timers.target
backup.service
[Unit]
Description=Daily Backup
StartLimitInterval=2000
StartLimitBurst=5

[Service]
Type=oneshot
Restart=on-failure
RestartSec=300
ExecStart=/bin/bash /root/backup.sh

Offline

#2 2021-08-09 06:14:45

flyingscorpio
Member
Registered: 2020-06-04
Posts: 34

Re: Systemd Timer Only When Wifi Is Connected?

Offline

#3 2021-08-09 07:49:14

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,365

Re: Systemd Timer Only When Wifi Is Connected?

Theoretically nice. Likely won't do in practice…
https://bbs.archlinux.org/viewtopic.php … 3#p1948193

If backup.sh requries some network condition (more likely access to some domain/IP) you could timeout there?

counter=6; while ((--counter > 0)) && ! ping -W5 -c1 example.com; do sleep 5; done

Offline

Board footer

Powered by FluxBB