Results 1 to 10 of 10

Thread: [SOLVED] Run a script every time network comes up after boot

  1. #1
    Join Date
    Nov 2007
    Location
    Wisconsin
    Beans
    1,139

    [SOLVED] Run a script every time network comes up after boot

    I want to run a script after networking comes up at boot, just like ntp does.

    The script is simple, it just refreshes my desktop. But how do I get it to work at startup? If I put it in /etc/network/if-up.d, nothing happens. Do I need to add a link somewhere?

    I'm running Xubuntu 8.04, with Network Manager, and all networking works just great.

  2. #2
    Join Date
    Mar 2007
    Location
    Denver, CO
    Beans
    7,726
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Run a script every time network comes up after boot

    Not sure if Network Manager is setup to run scrips after a connection is made unless manually configured. That would require manual configuration of the /etc/network/interfaces file and would be an entry similar to post-up ....etc. I know WICD has the ability to run scripts after making a connection, however to the best of my knowledge it also makes use of the post-up

  3. #3
    Join Date
    Jul 2006
    Beans
    Hidden!
    Distro
    Kubuntu 8.04 Hardy Heron

    Re: Run a script every time network comes up after boot

    There is a way to start script after Network Manager gets a connection, but it requires some python scripting.
    Not very elegant: separate process that waits for network connection to be established to run some scripts.
    This functionality should be built-in in NM - but I had something like this working:
    Code:
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import gobject, dbus
    from dbus.mainloop.glib import DBusGMainLoop
    import os
    
    # what to do on network device activation
    def on_device_now_active(*args):
            obj_path = args[0]
            net_name = args[1]
            #print "obj_path", obj_path
            #print "net_name", net_name
            if obj_path.endswith("eth1") and (net_name == "home_network"):
                    #print "Connected to:", net_name
                    #print
                    os.system("pon myvpn")
                    #os.system("skype &") # run skype in background
    
    # dbus events will be part of glib message loop
    DBusGMainLoop(set_as_default=True)
    # attach to D-BUS system bus and wait for DeviceNowActive event
    bus = dbus.SystemBus()
    proxy = bus.get_object('org.freedesktop.NetworkManager',
            '/org/freedesktop/NetworkManager')
    proxy.connect_to_signal("DeviceNowActive", on_device_now_active)
    # program main loop - waits for events and dispatches handlers
    loop = gobject.MainLoop()
    loop.run()
    but I actually didn't put it in desktop startup. Nevertheless, from console it works as expected.
    This code needs validating parameters for handler (I dunno if f.e. net_name is available for wired network).

  4. #4
    Join Date
    Nov 2007
    Location
    Wisconsin
    Beans
    1,139

    Re: Run a script every time network comes up after boot

    Thanks. I may try it.
    ntp seems to work upon network startup. Anyone have an idea how it does that?

  5. #5
    Join Date
    Dec 2007
    Location
    United States
    Beans
    2,900
    Distro
    Ubuntu

    Re: Run a script every time network comes up after boot

    I assume you mean that your network goes up and down in the course of a session (however long), otherwise you could put your command at the end of the run level start up. For example, when the commands in /etc/init.d/rc.local are run, the the network start phase has finished (S28NetworkManager).

  6. #6
    Join Date
    Dec 2007
    Location
    United States
    Beans
    2,900
    Distro
    Ubuntu

    Re: Run a script every time network comes up after boot

    Quote Originally Posted by Cheesehead View Post
    I want to run a script after networking comes up at boot, just like ntp does.
    Looking at the man page:

    There exists for each of the above mentioned options a directory
    /etc/network/if-<option>.d/ the scripts in which are run (with no argu
    ments) using run-parts( after the option itself has been processed.
    So, if you put your script in /etc/network/if-up.d (the directory) it will run with ntp and the rest.

    However, Kevdog implies that there may be another process of starting and stopping interfaces, which may negate this.
    Last edited by blueridgedog; January 3rd, 2009 at 04:45 AM.

  7. #7
    Join Date
    Nov 2008
    Beans
    9,635
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Run a script every time network comes up after boot

    Hi,

    This may be somewhat of assistance:

    http://www.arachnoid.com/linux/Netwo...ger/index.html

  8. #8
    Join Date
    Nov 2007
    Location
    Wisconsin
    Beans
    1,139

    Re: [SOLVED] Run a script every time network comes up after boot

    Placed the script in /etc/dhcp3/dhclient-enter-hooks.d
    Works perfectly upon network startup.

  9. #9
    Join Date
    Mar 2007
    Location
    Denver, CO
    Beans
    7,726
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: [SOLVED] Run a script every time network comes up after boot

    Glad you got this solved however this would only be applicable if you were running dhcp

  10. #10
    Join Date
    Nov 2007
    Location
    Wisconsin
    Beans
    1,139

    Re: [SOLVED] Run a script every time network comes up after boot

    I found another method that is easier to maintain - use Upstart events.

    When connecting to a network, if-up emits a 'network-device-up' upstart signal.

    I wrote an upstart listener to that simply runs my script(s) when it hears the signal. Since there are all kinds up upstart events, and I can add my own events, it's a convenient place to keep a lot of my event-based customizations.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •