Skip to content

Instantly share code, notes, and snippets.

@thorrr
Last active August 29, 2015 14:04
Show Gist options
  • Save thorrr/fcbd4af29556919c31d3 to your computer and use it in GitHub Desktop.
Save thorrr/fcbd4af29556919c31d3 to your computer and use it in GitHub Desktop.
Run this in a one minute cron job to check network connectivity every 20 seconds
#!/bin/bash
LOCKFILE=/tmp/check-and-restart.lock
if ! mkdir "${LOCKFILE}" 2>/dev/null; then
echo "check-and-restart is already running." >&2
exit 1
fi
trap "rm -rf ${LOCKFILE}; exit" INT TERM EXIT
CHECK_IP=8.8.8.8
for i in `seq 1 5`;
do
ping -c 10 $CHECK_IP > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo >> /root/restart.log
echo >> /root/restart.log
echo ------------------ >> /root/restart.log
echo Bouncing internet at `date` >> /root/restart.log
echo >> /root/restart.log
/sbin/ifdown eth0 >> /root/restart.log 2>&1
echo >> /root/restart.log
/sbin/ifup eth0 >> /root/restart.log 2>&1
echo >> /root/restart.log
echo ------------------ >> /root/restart.log
if [ $? -ne 0 ]; then
echo ifup didnt work for some reason >> /root/restart.log
exit 1
fi
exit 0
fi
sleep 10
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment