Add After=network-online.target
to the [Unit]
section of the timer.
Explanation:
Timers do accept all the relative ordering commands in the [Unit]
section that are known for services. In fact both the [Unit]
and [Install]
sections are identical for timers and services. Form the official manuals:
A unit configuration file whose name ends in ".timer" encodes information about a timer controlled and supervised by systemd, for timer-based activation.
This man page lists the configuration options specific to this unit type. See systemd.unit(5) for the common options of all unit configuration files. The common configuration items are configured in the generic [Unit] and [Install] sections. The timer specific configuration options are configured in the [Timer] section.
That said you need to know about network-online.target that defines if a network is up.
network-online.target is a target that actively waits until the nework is "up", where the definition of "up" is defined by the network management software. Usually it indicates a configured, routable IP address of some kind. Its primary purpose is to actively delay activation of services until the network is set up.
Limitations
network-online.target
is not checking for internet but for network connections. The LAN of course might not have internet access per se. If you cannot rely on the router or your ISP to provide a connection, you would have to make e.g. a special test-internet.service
that pings some website and only is defined active after it succeeded once (and otherwise restarts on failure every 15s or so). That should be a Type=oneshot
and RemainAfterExit=yes
kind of service. But I assume that this is not what you asked for.
Scopes: system/user
Be careful, as network-online.target
is a system scope unit.
Units in the user scope will not see it and fail to start. This can be fixed by creating a linked unit: systemctl --user link /lib/systemd/system/network-online.target
. The path can vary, the location can be checked by running: systemctl show --property FragmentPath network-online.target
sleep 5
at the top of your script? Might not be a solution, but with appropriate choice of sleep time should do as a workaround. – Peregrino69 Sep 16 '21 at 09:29