Linux Ping Script… (Finally…)

Anyone reading this may remember I was having intermittent – but regular and extremely annoying – network dropouts in my accommodation.

Well, I finally got twenty minutes (and a network switch so I can have more than one thing plugged in to the LAN at once) to get my act together and update my little Raspberry Pi 3 so it can sit and (low power) ping a server and let me know when the network drops out.

Just like the Windows version, this only writes to the log file when it can’t connect – so if your network never drops out, you’ll never even see the log file get created. You can test it by changing the sleep time and what it greps for (if you change sleep to 1 and grep unknown to grep PING, you can check it works…)

Just save it somewhere sensible, like /home/user/bin/scripts or similar, chmod +x it, and run it from the terminal. Force it to the background and you can log out and leave it ticking over.

#!/bin/bash
while true; do
     x=`ping -c1 www.google.com 2>&1 | grep unknown`
     if [ ! "$x" = "" ]; then
         date >> brokentimes-pingtest.txt
     fi
sleep 30
done