1

I'm writing a loop that checks whether a connection has been established.

I'm doing

ping -c 1 8.8.8.8
while [ $? -ne 0 ] do
  sleep 0.5
  ping -c 1 8.8.8.8
done

Now I'm not sure how this works, if the ping request got blocked on its way out (iptables or other) the command and the script will hang indefinitely. What I want is to stop waiting for a reply after 1 sec and send a new request. That until I get a reply faster than 1sec, the $? is equal to 0 and it breaks the loop.

ChiseledAbs
  • 2,193
  • 10
  • 28
  • 59

1 Answers1

3
while ! (ping -c 1 -W 1 8.8.8.8 > /dev/null); do
  sleep 1
done
echo "< 1 sec reply received...exiting"
user1700494
  • 2,172
  • 8
  • 13