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.

2 replies on “Check and restart vpnc VPN on Ubuntu Linux”

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 –

of course – /sbin/ifconfig
yeah, i’m a microsoft guy really. but thanks for the script anyhow, really helped me out.

Leave a Reply

Your email address will not be published. Required fields are marked *