12

Here is the content of /etc/network/interfaces:

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp

post-up /etc/network/if-up.d/sshstart

And sshstart is a script with the following in it:

curl something something darkside send a file over ftps in the background &
/usr/bin/autossh -M 0 -f -N -o ServerAliveInterval=15 -o ServerAliveCountMax=3 -R 127.0.0.1:2005:127.0.0.1:22 -R 192.168.1.10:2006:192.168.2.110:1912 -L 127.0.0.1:5249:192.168.1.212:5249 [email protected] -p 8080

When the machine reboots, the curl command is executed multiple times, the file ends up 2 or 3 times on the ftp server and when I look at the processes it seems like there are multiple instances of autossh running... Not sure if this is how autossh does things or not, but for sure curl shouldn't upload the file multiple times.

My hunch is that the whole sshstart script is ran multiple times but I don't understand why.

I tried searching for details on the network setup process at boot but all I could find was syntax information for the interfaces file.

Can someone help please?

Thank you.

---Edit---

As suggested bellow I have modified my interfaces file as follow (removed the empty lines above post-up):

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
post-up /etc/network/if-up.d/sshstart

And added the following line to sshstart:

echo $(date)>>/run/shm/sshstart.log

Here is the content of /run/shm/sshstart.log after a reboot:

Wed Oct 29 08:07:00 EDT 2014
Wed Oct 29 08:07:07 EDT 2014
Wed Oct 29 08:07:07 EDT 2014
Wed Oct 29 08:07:07 EDT 2014

So its been ran 4 times :( what's going on?

5
  • 1
    Hint: lo sets if-up.d to true, and eth0 also sets it to true. The post-up runs after each one is set. Your script needs to depend on only 1 interface being up, not both.
    – eyoung100
    Oct 28, 2014 at 18:53
  • I thought it could be something like that, guess its time to RTFM some more.
    – TCZ8
    Oct 28, 2014 at 20:26
  • I have checked around and I cant find how to specify which adapter for the post-up command, seems to have something to do with a "stanza" but I don't exactly get it. the Debian and Ubuntu interfaces page have quite a lot of info but they skipped that part.
    – TCZ8
    Oct 28, 2014 at 20:42
  • 1
    Not related to the question, but BTW echo $(date) is kinda awkward and indirect. Might as well just spell that as date. Similar to Useless Use of Cat Award.
    – Celada
    Oct 29, 2014 at 15:52
  • hahaha! you are absolutely right! I have no clue why I have done that :P
    – TCZ8
    Oct 29, 2014 at 21:16

4 Answers 4

15

Files in /etc/network/if-up.d already run automatically whenever an interface (any interface) comes up. When you specify the same script to run again in an explicit post-up command, you only cause the script to run again. So my guess is this is what should happen:

  • It runs once when lo comes up (with environment variable IFACE=lo) due to being located in /etc/network/if-up.d.
  • It runs once when eth0 comes up (with environment variable IFACE=eth0) for the same reason.
  • It runs again when eth0 comes up (with environment variable IFACE unset) because you asked for this in a post-up directive.

I'm not sure where the fourth time comes from, but anyway that's three already.

You need to either locate the script somewhere else and run it once using a post-up directive, or leave it where it is but don't mention it in a post-up directive and check the value of $IFACE so that it does nothing unless the desired interface (eth0) has come up.

3
  • OHH MAN! Finally! you are absolutely right, I just added a line to sshstart to echo $IFACE in my log file and its exactly as you described it! 1st time is lo, 2nd time is eth0, 3rd is eth0 again (form the post-up) and the 4th time is no so clear.. $IFACE's value on the 4th time is: --all You wouldnt happen to know what that means? Just by curiosity :)
    – TCZ8
    Oct 29, 2014 at 17:08
  • 1
    I didn't know about --all. It sounds like there is a feature to run the if-up.d scripts one last time with this argument to signal that all interfaces are now up, in case some script wants to take an action once it becomes known that no further interfaces are pending. That explains the fourth time.
    – Celada
    Oct 29, 2014 at 18:54
  • That makes sense, thanks for all your help!
    – TCZ8
    Oct 29, 2014 at 21:17
4

As stated by Celada, /etc/network/if-up.d scripts are run for each interface. To avoid rule duplication simply add:

[ "$IFACE" = "eth0" ] || exit 0

at the top of your script. This will cause the script to exit immediately if the interface is not the desired one.

1

Scripts in /etc/network/if-*.d are run for each interface, but also for each address family (IPv4 and IPv6).

You can check the variable $ADDRFAM if you don't want or need tĥe script to run for both IPv4 and IPv6 families.

For example, if you only need your script to run for a specific interface and only for it's IPv4 address, you could start with something like this:

[ "$IFACE"   = "eth0" ] || exit 0  # we only want interface "eth0"
[ "$ADDRFAM" = "inet" ] || exit 0  # we only want IPv4 (not "inet6" for IPv6)
0

Delete empty line before post-up-command. This should relate the post-up command to eth0 only. Additionally move your script out of if-up.d folder. Scripts in that folder are executed automatically, no matter if defined as post-up. In your case it will run additionally to your post-up-command.

1
  • Not working, see the edit in my question.
    – TCZ8
    Oct 29, 2014 at 12:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .