10

I'm using systemd-timer to periodically run a script which consumes a webservice.
Problem is, upon system resume or wake-up, internet connectivity would not get started right away but the timer gets fired and hence the script returns error (If the service waits for a couple of seconds, the script would run correctly and there would be no need to postpone the task until next run.)

1- How can I make it so that the timer (or the service associated with it), waits until net connectivity is available?

2- How can I make the timer (or service) not call the script when system is not online yet?

2
  • How about just adding something like 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. Commented Sep 16, 2021 at 9:29
  • 2
    freedesktop.org/wiki/Software/systemd/NetworkTarget Commented Sep 16, 2021 at 9:36

1 Answer 1

16

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

8
  • Thanks, do I also need to add Wants=network-online.target? Commented Sep 16, 2021 at 11:17
  • 2
    Wants will also make the timer try to start the network target. You may add it, but I think for your case it is not that necessary - since I assume you always start the network by default. If your connection is unstable, you may rather think of something like BindsTo=, i.e. stopping the timer if the network connection breaks. More details on the relative dependency options here: Service unit configuration Commented Sep 16, 2021 at 11:27
  • and what would be the difference if I add After=network-online.target to the Unit section of corresponding .service file (not .timer)? Commented Sep 16, 2021 at 11:29
  • 1
    Sorry for bothering again...actually my timer/service are defined in .config/systemd/user (user-level)...so I can't depend on network-online.target which is a system service, right? I need to write my own connection-detector service at user-level right? Commented Sep 17, 2021 at 14:57
  • 1
    No worries. - Yes, system level and user level are fully independent - see here or here. I'd suggest you make a small ping test service to some reliable internet address. Commented Sep 19, 2021 at 10:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.