Run script in Debian 6 when VPN is established

Posted on 2011-10-19
Last Modified: 2012-05-12
I'm a relative novice in the inner workings of Linux... so I apologize if this is a very basic question...

I have a Linux box running Debian 6 (Squeeze). One of the applications that runs at startup is OpenVPN which establishes a VPN connection (tun0). I have a script which I currently run manually after startup which establishes routing over the VPN - it is just a series of "ip route add" statements which point packets bound for various IP ranges to the VPN tunnel.

I'm trying to find a way to run this script automatically when the box starts but can't figure out how to do this. Also it sometimes takes up to 30 seconds for the VPN to establish and if the script runs too soon it fails because the system does not yet know about tun0.


<> Is there a way to get the script to run after an extended delay (say 60 seconds)?
<> Better, is there a way to get the script to run automatically when the tun0 interface comes up?
<> Or is there a better way of achieving what I'm trying to do?

I'm running the box in console mode - so no GUI.

Thanks for your help,

Regards, Chris
Question by:chrwil
    LVL 2

    Accepted Solution


    openvpn allows you to specify routes in the config file, just add in

    for example.

    for script delays, just put a "sleep 60" in the script.

    Author Comment


    Thanks Speculatrix. Adding to the ovpnxxx.conf file is a good solution.

    Do you know if there is any way of including another file into the conf file by reference (instead of inserting the long list of ip route add commands into the file itself)? I looked and couldn't see an option for this but it would be nice if there was.

    If I want to stick with running a standalone script (with the sleep line) then where do I put the script and how do I tell the system to run it? I more-or-less knew how to do this in Lenny but Squeeze seems to have changed things...

    Thanks, Chris

    Author Closing Comment

    Unfortunately I could not add all my routes to the ovpnxxx.conf file (my guess is that there is a limit to the size of the conf file or to the number of route commands that it can contain). My fallback was to run the separate script with sleep 60 as suggested - I managed to figure out how to use insserv to add this to the startup scripts.

    Thanks, Chris

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Logmein alternative 10 97
    Redundant Exchange Servers DAG? 5 39
    Linux : can't create transaction lock error 1 45
    IKEv2 VS  SSTP 4 45
    Secure VPN Connection terminated locally by the Client.  Reason 442: Failed to enable Virtual Adapter. If you receive this error on Windows 8 or Windows 8.1 while trying to connect with the Cisco VPN Client then the solution is a simple registry f…
    I've written this article to illustrate how we can implement a Dynamic Multipoint VPN (DMVPN) with both hub and spokes having a dynamically assigned non-broadcast multiple-access (NBMA) network IP (public IP). Here is the basic setup of DMVPN Pha…
    After creating this article (, I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
    After creating this article (, I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now