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:
- 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.
- 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.
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 thentpdate
package, then there is anntpdate
hook there. This is exactly the right point for what you want to do. – Gilles 'SO- stop being evil' Apr 11 '14 at 09:13ntpd
is an example, per the initial post referenced in my question) – WoJ Apr 11 '14 at 13:01dynamic
is the solution. Otherwise, there's no magic to it. A bash while loop is a reasonable solution if you need something simple; otherwise there are plenty of monitoring software that require a bit more setup but deliver more power and convenience. – Gilles 'SO- stop being evil' Apr 12 '14 at 20:13