Skip to content

Instantly share code, notes, and snippets.

@mohamad-supangat
Forked from bcantoni/icheck.sh
Created October 12, 2022 03:28
Show Gist options
  • Save mohamad-supangat/f97a476b3574e3b7edf328341130ee63 to your computer and use it in GitHub Desktop.
Save mohamad-supangat/f97a476b3574e3b7edf328341130ee63 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