Skip to content

if-up and if-down scripts with systemd

Everybody likes systemd, right? Well, not everybody. Who thought it might be a good idea to replace existing init systems with something which is big, monolithic, and not even feature complete?

A simple task: execute a script when an interface comes up, or goes down.

In the old days, on Debian or Ubuntu, one just added pre-up and post-down scripts in /etc/network/interfaces. But now, that file is gone, or empty. Ubuntu comes with netplan, which nobody else seems to use - luckily. NetworkManager is not used all the time, so one can't depend on that either. And systemd does not have an option to do something simple like taking care of interfaces coming up and down. You can write yourself a target, but that only fires for the first time, not every time.

In the end, you have to listen to D-Bus messages. And suddenly the simple task of running a simple script turns into developing a full-featured daemon with D-Bus capabilities.

Someone else already took the stab, and developed networkd-dispatcher.

 

On Ubuntu, the package is called networkd-dispatcher, and usually is already installed. Unfurtunately it does not provide an option to place scripts or executables somewhere in /etc, so one has to use /usr/lib/networkd-dispatcher. There are 4 directories:

  • dormant.d
  • no-carrier.d
  • off.d
  • routable.d

Here I'm only interested in routable.d and off.d. Another minor problem is that each script is executed whenever any interface comes up or goes down. So the script must check the interface name as well. The name is provided in $IFACE. Example:

#!/bin/bash

# must be /bin/bash, for some reasons /bin/sh does not work

if [ "$IFACE" == "eth0" ];
then
    # insert code here
fi

 

  • Twitter
  • Bookmark if-up and if-down scripts with systemd at del.icio.us
  • Facebook
  • Google Bookmarks
  • FriendFeed
  • Digg if-up and if-down scripts with systemd
  • Bloglines if-up and if-down scripts with systemd
  • Technorati if-up and if-down scripts with systemd
  • Fark this: if-up and if-down scripts with systemd
  • Bookmark if-up and if-down scripts with systemd at YahooMyWeb
  • Bookmark if-up and if-down scripts with systemd at Furl.net
  • Bookmark if-up and if-down scripts with systemd at reddit.com
  • Bookmark if-up and if-down scripts with systemd at blinklist.com
  • Bookmark if-up and if-down scripts with systemd at Spurl.net
  • Bookmark if-up and if-down scripts with systemd at Simpy.com
  • Bookmark if-up and if-down scripts with systemd at blogmarks
  • Bookmark if-up and if-down scripts with systemd with wists
  • wong it!
  • Bookmark using any bookmark manager!
  • Stumble It!
  • Identi.ca

Trackbacks

Netz - Rettung - Recht on : Wellenreiten 08/2018

Show preview
Wer als “Websurfer” metaphorisch auf den Wellen des Netzes reitet, findet dabei zwar keine paradiesischen Inseln, manchmal aber immerhin ganz interessante Lektüre. Im August 2018 kann ich u.a. folgende Fundstücke empfehlen und der werten Lese Comments ()

Comments

Display comments as Linear | Threaded

Yves on :

Thanks for this article, it was very helpful to my problem of adding additional IPv6 addresses to a server. Works fine when applying a netplan config or otherwise restarting the network and on system boot. The package networkd-dispatcher is already pre-installed on Ubuntu Server 20.04 and even in the main archive, so it should be a solid solution and well-supported by Ubuntu.
Comments ()

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications.
To leave a comment you must approve it via e-mail, which will be sent to your address after submission.
Form options