6

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

1 Answer 1

10

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.

8
  • Thanks, do I also need to add Wants=network-online.target? 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
    – FelixJN
    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)? Sep 16, 2021 at 11:29
  • Simple: If you do not define to start the timer after online target, the countdown will start earlier (e.g. when you defined it as WantedBy=multi-user.target in the [Install] section it will start at boot time - independently of the network status). If you define it in the service file, it will wait for the online target after triggering the service via the timer.
    – FelixJN
    Sep 16, 2021 at 11:37
  • BindsTo= option is only relevant in boot process? I mean, can I use it to disable the timer when network suddenly turns off and re-enable it when it becomes available? (After boot). I used BindsTo= and After= with network-online.target but when network came back online, the timer didn't reactivate Sep 16, 2021 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.