In a debian build, I have a custom init.d script. I've also modified my network so that the connection to the WiFi is done in the background.

How can I start my custom script, say custom.sh during boot up but have it actually run as soon as the network is brought up and connected?

Currently, I was thinking of looping every x seconds, and everytime check to see if the network is up. Once is it up, then you jump out of the loop.

  • 1
    Add After=network.target and Wants=network.target to the service file (if you are using systemd)... – jasonwryan Mar 18 '15 at 16:26
  • I don't think that will guarantee it runs "as soon as", just "afterward", but hopefully good enough. If you are using a sysvinit debian there's a similar mechanism there, see /etc/init.d/README. I'm not sure if the latter can be done on the basis of success though. – goldilocks Mar 18 '15 at 16:45
  • can you add it in /etc/network/interfaces – Milind Dumbare Mar 18 '15 at 16:57

On Debian, all methods to start and stop network connections run scripts under /etc/network. This is documented in the interfaces(5) man page. It is part of the ifupdown package, but the scripts are executed even if the interface is brought up or down by frameworks such as NetworkManager rather than by the ifup and ifdown commands.

So put your script in /etc/network/if-up.d. Make sure that it includes a check that the interface you wanted is up — in particular, you don't want your script to be executed as soon as the loopback interface goes up. You can check the name of the interface with the IFACE environment variable, either directly or by calling commands like ifconfig, route or ip to check its characteristics (IP address, gateway, …). Alternatively, you might check something independently of the interface, e.g. test that some host is reachable.

Note that scripts are executed in lexicographic order. If your script requires some other functionality (e.g. DNS) to be available, make sure that it comes later than the script that sets up this functionality.

  • I don't think that it would work as WLAN interface is up BEFORE being connected to network. – Flash Thunder Dec 7 '16 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.