Skip to content

Instantly share code, notes, and snippets.

@karlwilbur
Last active May 24, 2019 15:32
Show Gist options
  • Save karlwilbur/e6c7719a3a35d75892aa87921c96f1aa to your computer and use it in GitHub Desktop.
Save karlwilbur/e6c7719a3a35d75892aa87921c96f1aa to your computer and use it in GitHub Desktop.
Start/stop OpenVPN while tracking PID
#!/bin/bash
PID_FILE=~/run/openvpn.pid
if [ -e $PID_FILE ] && [ -s $PID_FILE ]; then
pid=`cat $PID_FILE`;
if [ ! "$(ps -eF | grep $pid | grep openvpn)" ]; then
echo "It seems that OpenVPN is not running and was not shutdown cleanly, remove the file '$PID_FILE' before attepting to start."
exit 1;
fi
echo "Stopping OpenVPN with PID: $pid";
sudo kill $pid && rm $PID_FILE;
else
echo "OpenVPN is not running."
fi
#!/bin/bash
PID_FILE=~/run/openvpn.pid
if [ -e $PID_FILE ] && [ -s $PID_FILE ]; then
if [ "$(ps -eF | grep `cat $PID_FILE` | grep openvpn)" ]; then
echo "It seems that OpenVPN is already running with PID $(cat $PID_FILE)";
exit 1;
else
echo "It seems that OpenVPN was not shutdown cleanly, remove the file '$PID_FILE' and try to start again."
exit 1;
fi
fi
sudo openvpn --daemon --config ~/.config/openvpn/karlwilbur.ovpn
pid=`pidof openvpn`
echo $pid > $PID_FILE
echo "OpenVPN started with PID $pid"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment