Run a service after networking is ready on Ubuntu?

6

5

I'm trying to start a service that depends on networking being started, whenever the computer is rebooted. I have a few questions:

  1. Is this easily possible from an /etc/init.d script? I have tried creating a script here (conforming to the standards), but I'm really doubtful that it's even running on boot, let alone working. When I test it manually, it works.

  2. I've seen the new Upstart service, but as far as how that actually works, I'm completely in the dark.

How can I make a script that runs on boot which runs after networking has been started? If I could run it after connected to wireless network, even better :)

Naftuli Kay

Posted 2011-02-21T22:30:20.867

Reputation: 8 389

Answers

6

You can use files in /etc/init as models. For example, this is /etc/init/mountall-net.conf:

# mountall-net - Mount network filesystems
#
# Send mountall the USR1 signal to inform it to try network filesystems
# again.

description     "Mount network filesystems"

start on net-device-up

task

script
    PID=$(status mountall 2>/dev/null | sed -e '/,/{s/.*,[^0-9]*//;q};d')
    [ -n "$PID" ] && kill -USR1 $PID || true
end script

and this is '/etc/init/ufw.conf`:

# ufw - Uncomplicated Firewall
#
# The Uncomplicated Firewall is a front-end for iptables, to make managing a
# Netfilter firewall easier.

description     "Uncomplicated firewall"

start on net-device-added INTERFACE=lo
stop on runlevel [!023456]

console output

pre-start exec /lib/ufw/ufw-init start quiet
post-stop exec /lib/ufw/ufw-init stop

The latter file has a symlink in /etc/init.d:

$ ls -l /etc/init.d/ufw
lrwxrwxrwx 1 root root 21 2009-11-05 00:14 /etc/init.d/ufw -> /lib/init/upstart-job

Paused until further notice.

Posted 2011-02-21T22:30:20.867

Reputation: 86 075

Should I start on net-device-up or on net-device-added? I'm using this script to start a DLNA server serving over wlan0. Thanks for the tip, I'll have to find a good guide on all of the options available for Upstart, seems like it's years ahead of system v. – Naftuli Kay – 2011-02-21T23:28:59.843

1

@TKKocheran: Since you probably need communication to be available, net-device-up would probably be the way to go. Here's the reference. Look particularly at the wiki.

– Paused until further notice. – 2011-02-22T00:05:24.757

1

Darokthar

Posted 2011-02-21T22:30:20.867

Reputation: 1 361