technical notes

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.

Following, James Coyle’s instructions, but changing the location of the script (because of this thread):

1. Make a script which checks vpn status and restarts it if necessary

1.1. Create file (at the terminal enter)
sudo nano /usr/local/sbin/
1.2. Copy and paste this into the file
if 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/

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/
Save and exit

And we’re done. You can also worry about where your crontab logs are, and how to check they are rotated daily (so don’t grow infinitely in size). This should be set up as default, so you don’t need to worry about it.