2

I have a linux box connected to a router via an ethernet cable. The cable is never unplugged and the linux box is always on. My goal is to have a command executed on the linux box every time the router is rebooted.

This question is quite similar to this question but the suggested solution doesn't seem to work. More specifically:

  • the command inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected' does never detect a change (even in the case the cable is unplugged from the pc), because according to this answer the command inotifywait does not work in /sys
  • the command (suggested in a comment) inotifywait -e modify /etc/network/if-up.d/avahi-daemon does not detect any change when the router is rebooted
Community
  • 1
  • 1
brad
  • 133
  • 1
  • 5
  • You may better help from SuperUser.. voting to move there – Jayan Apr 08 '15 at 12:25
  • How do I move the question to SuperUser? – brad Apr 08 '15 at 12:51
  • what is the reason for this? do you want to check if your external IP has changed? – Alex Apr 08 '15 at 13:01
  • Actually, this question belongs on [unix.SE](http://unix.stackexchange.com/) and I would love to see the answers that wonderful community would come up with for this question. – dotancohen Apr 08 '15 at 13:33
  • @Ixer: the reason is that the pc runs a vpn client: when the router goes down and then up again I want to restart the vpn connection automatically (the router is shut down every night and turned on every morning, while the pc is always on) – brad Apr 08 '15 at 14:01

2 Answers2

1

You can start a script after the linux box gets connected using up (requires ifplugd to be installed )

 #/etc/network/interfaces
auto eth0
    iface eth0 inet dhcp
    up /etc/network/yourscript.sh

However, there keep in mind that if you disconnect the cable (and plug in in after a while), the script also starts even though the router might not have been restarted.

--edit--

alternatively , place your script in

/etc/network/if-up.d/ (make sure it is executable and restart networking after changes.)

Alex
  • 5,759
  • 1
  • 32
  • 47
  • @Ixer: I already tried with just "up" and it didn't work. Maybe "post-up" was the right one: I'll give it a try – brad Apr 08 '15 at 14:03
  • for some reason it doesnt work on my ubuntu machine either, even though this used to be the correct way. (i blame 'networkmanager', it sucks. `wicd` has better scripting options ). Try putting your script in `/etc/network/if-up.d/` – Alex Apr 08 '15 at 16:23
  • If an interface is in `/etc/network/interfaces`, then it is not managed by NetworkManager (after NM is restarted). From my experiments, `up` and `down` script are triggered only if the interface is managed by `ifup/ifdown`. I have been able to get this installing the `ifplug` daemon. I will test that for a few days to see if it works. – brad Apr 08 '15 at 19:38
  • @Ixer: it looks like it works, provided the ifplug daemon is install and manages eth0. So, if you edit your answer adding this (and changing `post-up` to simply `up`, then I will accept your answer – brad Apr 13 '15 at 10:42
0

Depending on the Linux distribution on that router the correct way of running a command at startup/reboot is to create a startup script, or add the command to /etc/rc.local

nbkr
  • 121
  • 5
  • Adding the command to /etc/rc.local is the easiest way - it's the last thing to run in all of your runlevels before providing a login prompt. – calmond Apr 08 '15 at 13:04
  • 1
    there is most likely no access to this if it is a simple router. The goal was to execute a command on the linux box, not on the router itself. – Alex Apr 08 '15 at 13:12