Internet Connection Down? Get a Voice Alert When Your Mac is Back Online
We’re all incredibly reliant on our internet connections these days, but sometimes those connections aren’t so reliable. Whether it’s due to an ISP failure, a router being down, someone tripping over a cord somewhere, or whatever other occurrence, it can be frustrating to wait for a downed internet connection to come alive again. We’ve all known users who simply hit the “refresh” button on a web browser repeatedly to see when things revive, but there is a better way. Users who are comfortable with the command line can use a better solution, which utilizes the ping tool to detect an active internet connection and the text-to-speech say command to verbally announce when things are live again. Be sure to have the volume turned up on the Mac to use this.
Speak an Alert when Internet Connection is Online
This command will run into a successful ping to the domain “yahoo.com” is connected. By switching the domain with another, you could also use this to monitor if a web site or server is up or down.
until ping -W1 -c1 yahoo.com; do sleep 5; done && say the internet is back
If you’re not concerned about connecting to a resolved domain name (i.e.: somedomainname.com instead of 127.0.0.1), you could also ping an IP directly, which is what the original trick source suggested:
until ping -W1 -c1 8.8.8.8; do sleep 5; done && say the internet is back
A potential issue with pinging an IP directly is that the command may return a live connection when there continue to be DNS issues. Thus the first option, which pings a resolved domain name instead, may be a better option, because not only does it detect an active internet connection but it also only triggers when domain names are resolving properly as well.
This is similar to a command line trick we covered that announces the completion of a task through speaking a defined phrase, which can be helpful when a lengthy script is running or some other task may take an indeterminate amount of time to finish where an alert would be helpful.
Silent Alternative: Alert Internet Connection Online with a Photo
For users who are working in environments where having volume on or sound up is not an option, you can adjust the command to trigger another alert action. For example, replacing the ‘say’ command with ‘open’ would allow a picture to be opened with Preview to indicate an active connection has returned:
until ping -W1 -c1 yahoo.com; do sleep 5; done && open ~/BACKONLINE.jpg
In this case, when ping succeeds in contacting yahoo, an image named “BACKONLINE.jpg” will open in Preview app (or whatever the default image app is set to for the file type defined).
Heads up to @sedovsek on Twitter for this great trick, don’t forget to follow us there too.
This is really cool! I was looking for an app to do this!
Voice alert for Internet Down or back on! How cool!
It is 2021 and there isn’t an App for this….yet!
This is a really old thread but I liked what I saw and modified this for more modern systems with a pop-up dialog to warn against outages. Run in crontab every X minutes.
#!/bin/bash
# 8.8.8.8 is a google name server, it should always be pingable
HOST=8.8.8.8
DATE=`/bin/date +%m.%d.%Y-%T`
# no ping request
COUNT=10
for myHost in $HOST
do
count=$(ping -c$COUNT -q $myHost | awk -F’, ‘ ‘/received/ { print $2 }’ | awk ‘{ print $1}’)
if [ $count -eq 0 ]; then
osascript -e ‘tell app “System Events” to display dialog “‘$HOST’ is not pingable at ‘$DATE'”‘
fi
done
Is there a possibility to get an alert when connection goes down?
You could easily script one. The following was adapted from a script found elsewhere:
#!/bin/bash
# 8.8.8.8 is a google name server, it should always be pingable
HOST=”8.8.8.8″
# no ping request
COUNT=1
for myHost in $HOST
do
count=$(ping -c $COUNT $myHost | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)
if [ $count -eq 0 ]; then
# 100% failed
say “Uh-oh, the internet is down.”
fi
done
or even pop-up a window with the notification, add a timestamp, etc:
#!/bin/bash
# 8.8.8.8 is a google name server, it should always be pingable
HOST=”8.8.8.8″
DATE=`/bin/date +%d.%m.%Y-%T`
# no ping request
COUNT=1
for myHost in $HOST
do
count=$(ping -c $COUNT $myHost | grep ‘received’ | awk -F’,’ ‘{ print $2 }’ | awk ‘{ print $1 }’)
if [ $count -eq 0 ]; then
# 100% failed
osascript -e ‘tell app “Finder” to activate’ -e ‘tell app “Finder” to display dialog “The internet went down at ‘$DATE'”‘
fi
done
Add that script in a crontab to run every minute, and you’ll get a pop-up when it drops. Keep in mind with the second example, it won’t perform another ping again until you hit ‘OK’ on the dialog window that opens.
I would add volume 5 or something to it as well, so it auto sets the volume if its muted or low. And maybe even throw in the voice to use.
until ping -W1 -c1 yahoo.com; do sleep 5; done && osascript -e “set volume 5” && say -v Alex The internet is back up!
or
until ping -W1 -c1 yahoo.com; do sleep 5; done && osascript -e “set volume 5” && osascript -e ‘say “The internet is back up!” using “Whisper”‘
Even better you can have a proper osx notification, it’s pretty easy with terminal-notifier..
terminal-notifier is a great tool, it ties alerts to the system-wide Notification Center as discussed here: https://osxdaily.com/2012/08/03/send-an-alert-to-notification-center-from-the-command-line-in-os-x/