Internet connection drop alert
You can use a modified version of this script to do what you want:
#!/bin/bash
downTime=0
lastAccessTime=$(date +"%s")
while [ true ]; do
if ! ping -c1 google.com >& /dev/null; then
downTime=$(( $(date +"%s") - $lastAccessTime ))
else
downTime=0
lastAccessTime=$(date +"%s")
fi
sleep 15
if [ $downTime -ge 300 ]; then
echo "alert"
fi
done
We're "CONNECTED" Example
With debugging turned on so you can see what the script's doing.
set -x
Running with a valid hostname to demonstrate the "connection is up" state.
$ ./watcher.bash
+ downTime=0
++ date +%s
+ lastAccessTime=1402276955
+ '[' true ']'
The above initializes a couple of variables and determines the last time we went through the loop, $lastAccessTime
. We now try to ping Google.
+ ping -c1 google.com
+ downTime=0
++ date +%s
+ lastAccessTime=1402276955
We now calculate any down time, $downTime
, if ping fails, otherwise, we reset $downTime
to zero, and recalculate $lastAccessTime
.
+ sleep 15
Now we wait 15 seconds.
+ '[' 0 -ge 300 ']'
Now we check if we've been down for > 5 minutes (300 seconds). Then we repeat going through the while
loop.
+ '[' true ']'
+ ping -c1 google.com
+ downTime=0
++ date +%s
+ lastAccessTime=1402276970
+ sleep 15
....
As long as we're up, nothing will happen other than we check with the ping
command every 15 seconds.
We're "DISCONNECTED" Example
Now to simulate a "connection is down" state, we'll swap out the hostname we're pinging and use a fake one, google1234567890.com
. Repeating a run of our script with debugging enabled we now see some actual down time getting calculated.
$ ./watcher.bash
+ downTime=0
++ date +%s
+ lastAccessTime=1402277506
+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=0
+ sleep 15
+ '[' 0 -ge 300 ']'
+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=15
+ sleep 15
...
Notice above that $downTime
is equal to 15 seconds so far. If we wait a while longer we'll see this:
+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=300
+ sleep 15
We've accrued 300 seconds of down time. So now when we check, we print the message, alert
.
+ '[' 300 -ge 300 ']'
+ echo alert
alert
+ '[' true ']'
+ ping -c1 google1234567890.com
++ date +%s
+ downTime=315
+ sleep 15
This state will continue until the connection is restored and the ping
is once again successful.
So what about a sound?
That's easy. You can use a variety of tools to do this. I would use something like sox
or mplayer
to play an audio file such as an .mp3
or .wav
file with an appropriate sound you want to hear every 15 seconds, while the connection is down.
mplayer someaudio.wav
Simply replace the alert
message above with this line to get audio feedback that the connection is down.
Timing out issues with ping
If you use ping
in the manner above you'll likely encounter a slow lag time where it takes ping
literally 10-20 seconds for it to fail when the connection is down. See my answer to this U&L Q&A titled: How to redirect the output of any command? for an example using the command line tool fing
instead. This tool will fail more quickly than the traditional ping
.
if you are using Linux you can use ping within a script to google.com
for example:
counter=0
while [ true ]
do
ping -c 1 google.com > /dev/null 2>&1
if [ $? -ne 0 ]
then
let "counter +=1"
else
let "counter = 0"
fi
if [ $counter -eq 300 ] # we assume that one ping need one second (300 is 5 minutes)
then
echo "alert"
fi
done