brad
brad

Reputation: 153

Linux: execute a command when network connection is restored

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:

Upvotes: 2

Views: 4479

Answers (2)

Alex
Alex

Reputation: 6037

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.)

Upvotes: 1

nbkr
nbkr

Reputation: 131

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

Upvotes: 0

Related Questions