Skip to content

Instantly share code, notes, and snippets.

@bcantoni
Created July 2, 2018 19:42
Show Gist options
  • Save bcantoni/bffa952959c872059f9c882c5fa2a139 to your computer and use it in GitHub Desktop.
Save bcantoni/bffa952959c872059f9c882c5fa2a139 to your computer and use it in GitHub Desktop.
Bash script for checking internet connection status and keeping a local log when it changes. Meant to be run at some regular interval as a cron job.
#!/bin/bash
# check internet connectivity status
# file 'status' contains last known status (online or offline)
# file 'status.log' has an entry every time status changes
source ./status
echo "last status: $status at $time"
if nc -zw1 google.com 443; then
echo "online"
if [[ "$status" = "online" ]]; then
exit 0
fi
echo "we are back"
status="online"
else
echo "offline"
if [[ "$status" = "offline" ]]; then
exit 0
fi
echo "we are down!"
status="offline"
fi
# if we're here, it means status has changed
tm=$(date +%s)
dt=$(date -R)
cat > status <<EOF
status=$status
time=$tm
EOF
delta=$((tm-time))
echo "$status at $dt ($delta seconds)" >>status.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment