Sunday, February 6, 2022

Make sure a process is running?

I have a openvpn tunnel up normally for certain things and want to kill some processes
if the tunnel goes down…

Check every minute

* * * * * /root/bin/tun.up.sh >> /var/log/tunnel.up.log

Which checks for the tunnel tun0 and kill some processes if it’s not.

IFCONFIG=/sbin/ifconfig
GREP=/usr/bin/grep
ECHO=/usr/bin/echo
AWK=/usr/bin/awk
PS=/usr/bin/ps
PKILL=/usr/bin/pkill
DATE=`/usr/bin/date +"%m.%d.%y.%H.%M"`
PGREP=/usr/bin/pgrep

TEST=`$IFCONFIG | $GREP tun | $AWK '{print $1}'`
TOKILL=`$PS | $GREP ktorrent`

if [ $TEST == "tun0:" ]; then
 $ECHO $TEST " UP " $DATE
 exit
fi

PROC=`$PGREP ktorrent`
#$ECHO "Killing " $PROC
$ECHO $TEST " DOWN " $DATE
$PKILL -f ktorrent

Going to write a bit more code in the near future to bring the tunnel back up when it goes down
but that’s something for a different day.