Debian – How to Run a Script on Startup When Internet is Ready

debiannetworkingstartup

I am running Debian 7 Wheezy and I need to start some screens on startup as soon as there is a fully functional internet connection. However, not, if the internet connection broke and was connected again. So only on the first functional internet connection after boot.

Could you please post a dummy script for this and tell me where to put it and make it be executed under the given conditions?

The script only needs to start the screen and then terminate but the screen should continue.


EDIT
I have already heard of the /etc/network/if-up.d/ folder. But how can I make sure that the script is not executed again if the internet connection is lost and then re-established?

Best Answer

Put your script in /etc/network/if-up.d and make it executable. It will be automatically run each time a network interface comes up.

To make it do work only the first time it is run on every boot, have it check for existence of a flag file which you create after the first time. Example:

#!/bin/sh

FLAGFILE=/var/run/work-was-already-done

case "$IFACE" in
    lo)
        # The loopback interface does not count.
        # only run when some other interface comes up
        exit 0
        ;;
    *)
        ;;
esac

if [ -e $FLAGFILE ]; then
    exit 0
else
    touch $FLAGFILE
fi

: here, do the real work.
Related Question