The basic Debian way:
Basically /etc/network/if-up.d
is the place for such scripts. These scripts are called when an interface get's up on a Debian system or derivate - like Ubuntu. Here you can find a list of environment variables passed to these scripts. Using these variables you can decide which interface get's up and details of the interface's configuration. You may find examples of such scipts when looking into /etc/network/if-up.d
A sample script could look like this (derived from samba package. Just to explain how things work. If you are using Ubuntu you should use upstart
will be explained later)
/etc/network/if-up.d/your-service
#!/bin/sh
# Try to bring nmbd up when an interface comes up, if smbd is already running.
# Don't bother to do anything for lo.
if [ "$IFACE" = lo ]; then
exit 0
fi
# Only run from ifup.
if [ "$MODE" != start ]; then
exit 0
fi
# we only care about inet and inet6.
case $ADDRFAM in
inet|inet6|NetworkManager)
;;
*)
exit 0
;;
esac
# start the service
invoke-rc.d your-service start >/dev/null
exit 0
Note that you need to
chmod +x /etc/network/if-up.d/your-service
Ubuntu / Upstart:
Ubuntu is using upstart
as a sysvinit replacement. If you are looking into /etc/network/if-up.d/
you see that upstart
has placed a hook there as well. This hook is the base of the more elaborated upstart
mechanism that provides the possibility to start a service not unless a certain network interface has been brought up. The upstart hook will trigger the event net-device-up
and passes the IFACE
, LOGICAL
, ADDRFAM
and METHOD
as parameters. You use this hook in the upstart script for your service (like @Guy mentioned):
start on net-device-up IFACE=eth0