Have Mac OS X alert you when you are back online

Network connection down? Want Mac OS X to announce to you when you are back online? Turn up the volume; start Terminal (by clicking on the Spotlight magnifying glass in the upper right of your screen, typing ‘terminal’, and hitting Return); type or paste the following into the window; and hit Return:

while true; do ping -c 1 8.8.8.8 && say “Back online” && break; done

This will speak the words “Back online” to you when your network connection is reactivated.

Why this works

  • while true; do <some command>; done will repeat the specified command until it hits a break statement
  • ping -c 1 <some IP address> will send one packet to the specified IP address, returning true (if the server responds) or false (if the server cannot be reached or fails to respond)
  • 8.8.8.8 is one of Google’s public DNS servers, which should always be online and always respond to pings
  • say "<something>" will use the built-in text-to-speech synthesizer to speak the given text aloud and return true
  • break will exit the infinite loop
  • && chains commands, executing each only if the preceding command returned a value of true

Putting these together, this command will send one packet at a time, forever, to a server that should always respond, and the first time the server responds, it will tell you that you are back online and stop checking.