Check and restart vpnc VPN on Ubuntu Linux

I run Ubuntu 16.04 Linux and use vpnc for my vpn (which runs at the command line, rather than through the network manager). This link tells you how to run vpnc at startup, but really I wanted to have the vpn monitored and automatically restarted if it drops. Here’s how I did that.

1.1. Create file (at the terminal enter)sudo nano /usr/local/sbin/checkvpn.sh
1.2. Copy and paste this into the file#!/bin/bashif ifconfig tun0 &> /dev/null; then echo "---VPN active---"; else sudo vpnc; fi
Save and exit. The first line marks the file as a script. The second does the work, which I stole from here
1.3. Make this file executable (at the terminal enter)sudo chmod +x /usr/local/sbin/checkvpn.sh

2. Now create a cron job to run this script every minute
1.1. Open root cron jobs list (at the terminal enter)sudo crontab -e
1.2. Add this line to the end*/1 * * * * /usr/local/sbin/checkvpn.sh
Save and exit

thanks this helped me out on the same problem I was having. One issue I had though with your script. it worked as expected when I tested it from a terminal session, but when I ran it under cron, it was not working as expected and trying to start the VPN every time, if it was up or not. turns out it was because I needed to reference path to ifconfig – \sbin\ifconfig – turns out cron doesnt have /sbin in it’s default path – https://ubuntuforums.org/showthread.php?t=1917504