Skip to content

Instantly share code, notes, and snippets.

@ayanamist
Last active December 9, 2015 16:28
Show Gist options
  • Save ayanamist/4296874 to your computer and use it in GitHub Desktop.
Save ayanamist/4296874 to your computer and use it in GitHub Desktop.
vpncwatch (shell version)
#!/bin/sh
log() {
logger -t vpncwatch $@
}
SH_PID="/var/run/vpncwatch.pid"
VPNC="/usr/sbin/vpnc"
VPNC_OPTS=""
VPNC_PID="/var/run/vpnc/pid"
VPNC_LOG="/var/log/vpncwatch"
PROBE_IP="199.59.148.10"
# single instance
if kill -0 $(cat $SH_PID 2>/dev/null) 2>/dev/null; then
log "only one instance can be running"
exit
fi
# fork to background daemon
if [ -z "$_BACKGROUNDED" ]; then
_BACKGROUNDED=1 nohup $0 $@ >> $VPNC_LOG 2>&1 &
exit
fi
echo $$ > $SH_PID
start_vpnc() {
$VPNC $VPNC_OPTS
sleep 1s # give some time to connect
}
stop_vpnc() {
killall -w vpnc 2>/dev/null
}
sig_handler() {
log "exit"
stop_vpnc
rm $SH_PID 2>/dev/null
exit
}
# kill vpnc properly when being terminated
trap sig_handler exit INT TERM
while true; do
# check /opt has been mount
if [ -f $VPNC ]; then
# check vpnc is running
if kill -0 $(cat $VPNC_PID 2>/dev/null) 2>/dev/null; then
# check vpnc connection
if ping -q -W 2 -c 5 $PROBE_IP > /dev/null; then
:
else
log "vpnc died, restarting"
stop_vpnc
start_vpnc
fi
else
log "starting vpnc"
start_vpnc
fi
fi
done
@ayanamist
Copy link
Author

Should be run in a cronjob or any other than ssh, since shell in tomato will exit completely including all nohup process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment