2

I need to start a service (namely ntp) only when true Internet connectivity is in place (not only a link up). I seem to have noticed that some startup scripts delay themselves when a condition is not met (usually until a network device is available).

How to do correctly do this this in a startup script?

  • the startup script would run a check (another script)
  • based on the result it would either actually start the service or be delayed for some time, and re-execute the check

I can imagine a bash while condition but the script would be blocking and before jumping into backgrounding I wanted to make suere that there is no out-of-the-box solution, built-into the startup mechanism and environment.

Note: this is a followup to a previous question about startup script dependencies.

0

Starting a script at boot time and having it wait for the network to come up would be a weird and complicated way of doing what you want, which is to run something when the network comes up.

If you want to do something when the network comes up, then add it to the network startup scripts, not to the system startup scripts. Systemd and Upstart unify these, but Debian wheezy uses a traditional init system, which only manages startup (and runlevel changes).

Under Debian and derivatives, you can put a script to run when an interface comes up in /etc/network/if-up.d/.


If you need to monitor Internet connectivity as opposed to network connectivity, you need to do several things:

  1. Pick a definition of network connectivity. The Internet is not centralized; you can only check whether a particular host is responding to a particular request. This can be affected by firewalls along the way.
  2. Decide how long the network can be down before you react. This depends on what you want to achieve. If you pick a too short interval, a simple dropped packet will make you wrongly decide that the network is down. If you pick a too long interval, you won't detect failed connectivity fast.

There's no magic way to accomplish this. The most obvious way is to contact the server chosen for (1.), in a loop which sleeps for a time chosen for (2.) between contact attempts. This is the only way to detect when connectivity starts. There are other ways to observe loss of connectivity, such as opening a TCP connection and sending packets from time to time on that opened connection.

There are three possible approaches:

  • Put a crontab that tests connectivity at regular intervals, saves the state in a file and reacts when the state change.
  • Have a shell (or other) script running forever that loops forever around the connectivity test.
  • Use a more advanced monitoring tool.
  • "... complicated way of doing what you want, which is to run something when the network comes up": this is not what I want. I mentioned that in my question: "only when true Internet connectivity is in place (not only a link up)" – WoJ Apr 11 '14 at 7:22
  • @WoJ The link-up process starts the way towards Internet connectivity. In the if-up.d hook, the interface has an IP address, there's an early hook that ensures that any new DNS is in place. If the interface is providing Internet connectivity, then you do have Internet connectivity at that point. For example, if you install the ntpdate package, then there is an ntpdate hook there. This is exactly the right point for what you want to do. – Gilles Apr 11 '14 at 9:13
  • no, what I have is network connectivity, not Internet. In the case you start a whole environment at once some of the elements between your card and the first Internet hop may not be in place (which is my case - they would usually start later than my Debian box). This is why I need an Internet (and not network) connectivity check for some services. I know that they should handle the lack of Internet and recover accordingly but this is not always the case (my ntpd is an example, per the initial post referenced in my question) – WoJ Apr 11 '14 at 13:01
  • @WoJ Ah, ok. That's a different question. How do you determine Internet connectivity? Ping a particular server? And how do you want to react to a loss of Internet connectivity (e.g. your ISP messes up) and its reestablishment? – Gilles Apr 11 '14 at 13:27
  • Well, the question was explicit :) I am wondering how to correctly (according to best practices) delay the start of a service until Internet connectivity is back ("Internet connectivity" being for instance a successful ping to google.com, or whatever someone decides the right thing to be for him). I am particularly interested in how some scripts are delayed until "something happens" and whether this is a standard procedure (I doubt so) or everyone is inventing his own way. – WoJ Apr 11 '14 at 14:52

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.