Bash Tricks: Deal with a Flaky Internet Connection

Its been about 9 months in Vietnam now. One problem that I have to deal with on a day to day basis here is the flaky Internet connection. There are good days and bad days and bad days are incredibly frustrating. The Internet is ON and OFF every five minutes. I wrote this simple script which will wait for the Internet to be active before executing commands given to the script as an argument. I have found this very useful for commands like git pull/push, svn update etc.

#!/bin/bash
COMMAND="$@"
PING=$(ping -q -c 1 8.8.8.8 &>/dev/null; echo $?)
while [ $PING -gt 0 ]; do
  PING=$(ping -q -c 1 8.8.8.8 &>/dev/null; echo $?)
done
$COMMAND

I call it "wfi", wait-for-internet. Dump this script into your bin directory and give it executable permissions.

To test this script, disconnect your wifi/ethernet connection and then run the following command.

wfi ls -l

Reconnect your wifi/ethernet, and you'll see that the command gets executed as soon as your Internet connection is up.

wfi git pull

Now I can execute these commands through wfi and then go back to my work and be less exasperated.

About Pratik Sinha

Linux Nerd, Socialist, Atheist, Adventuristic, Nature Lover, Geeky.

Leave a Reply