On Linux, ping
has inconsistent behaviour. If it initially has no network connection, it terminates with the message
user@machine:~$ ping 8.8.8.8
connect: Network is unreachable
user@machine:~$
However, if there is a connection but it is disconnected, it keeps attempting to ping:
user@machine:~$ ping 8.8.8.8
64 bytes from 8.8.8.8: icmp_seq=1 ttl=120 time=37.4 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=120 time=37.4 ms
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
ping: sendmsg: Network is unreachable
64 bytes from 8.8.8.8: icmp_seq=6 ttl=120 time=37.8 ms
64 bytes from 8.8.8.8: icmp_seq=7 ttl=120 time=35.1 ms
* ... etc ... *
I often use ping
to monitor the status of my internet connection, especially on flaky Wifi, and it would be great to have ping "keep trying" if there is initially no connection. (I tried this on Mac OSX where ping
behaves as I would like it to.)
A simple while
loop isn't sufficient because ctrl-C handling doesn't work properly.
How can ping
be set up on Linux to keep retrying even if there is initially no connection?