RPI OpenVPN PIA Connection Check Cron Script

This script follows on from our previous post RPI Deluge Port Forwarding Script

This script will check your OpenVPN connection is still working correctly periodically and if not it will restart the openvpn connection.

It will also automatically update your Private Internet Access (PIA) Port Forwarding.

Please note: This script will only work if you have already created the port forwarding script under /home/pi/ as outlined in the previous blog post
RPI Deluge Port Forwarding Script. It also assumes you are running Deluge Daemon on Raspberry Pi with Open VPN.

  • Login to your RPI
  • cd /home/pi/
  • sudo nano vpncheck.sh
  • paste in the following script
# HTGSD.com 19/05/2019

#Check Connection - change google.com to whatever address you wish to check
count=$( ping -c 3 google.com | grep icmp | grep bytes | wc -l )

if [ $count -eq 0 ]
        service openvpn restart
        echo "VPN down, restarting"
        sleep 10
        #forward pia port to deluged
        /bin/bash /home/pi/portforward.sh

        echo "VPN still working, no action"
        #forward pia port to deluged
        /bin/bash /home/pi/portforward.sh
  • Save the file
  • make the file executable
    sudo chmod +x vpncheck.sh
  • check the script works
  • If it’s working you should see either:
    “VPN down, restarting”
    “VPN still working, no action”
  • The above will be followed immediately by the Port Forwarding script updating your Deluge port

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.