Last active
August 29, 2015 14:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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