Friday, March 27, 2009

keeponline.sh (Yet another one)

I have posted two versions of my keeponline.sh script. (The one that checks if I have been disconnected from the net and reconnects)

Here's the latest one. It is now capable of checking if one instance is already running, and won't start another one unnecessarily:

#!/bin/bash
#echo "Going Online..."
#/usr/local/bin/online.sh > /dev/null
LOCKFILE="/var/lock/keeponline.lock"
trap "{ echo $0 removing lock; rm -f $LOCKFILE; exit 255; }" SIGINT SIGTERM
if [ -f $LOCKFILE ]; then
echo "$0: already running, exiting..."
exit 1
fi
touch $LOCKFILE
while true; do
ping -c 2 -W 120 www.google.com > /dev/null

if [ "$?" = "0" ]; then
echo "You are connected"
else
/usr/local/bin/online.sh > /dev/null
echo "Disconnect detected at `date`, reconnecting" >> /home/akshay/logs/keeponline.log
fi
sleep 10m
done


There is no real use for this script, since SuperSify provides this feature. But I found that my version has less number of false positives when the bandwidth is completely choked. Besides, I just wanted to learn how to use traps :)

No comments: