1

I have the following script that I need to run on my Synology NAS at boot-up. The Synology NAS has built-in scheduled tasks with the ability to trigger tasks at boot-up.

The script I need to run is this:

#!/bin/sh

ip link add macvlan0 link eth0 type macvlan mode bridge
ip addr add 192.168.0.240/32 dev macvlan0
ip link set macvlan0 up
ip route add 192.168.0.240/28 dev macvlan0

However, this does not work, the task seems to trigger to soon when the eth0 interface is not yet ready to accept these commands.

Placing a sleep 60 before all ip * commands, solves the issue. But it doesn't feel right to wait an arbitrary amount of seconds before doing my thing.

Given my requirements above, what's the best method to wait for the eth0 interface to be available so that I can execute ip * commands?

  • instead of sleep 60, try running ip monitor link dev eth0 | grep -q 'state UP'. – mosvy Apr 7 at 12:46
  • or if that doesn't work either, ip monitor link | grep -q 'eth0:.*UP,LOWER_UP'. – mosvy Apr 7 at 12:55
  • @mosvy Both commands (without the grep) don't seem to return anything, it keeps waiting for output... – Ricardo Amaral Apr 7 at 17:39
  • What were you expecting them to return? They should only trigger when the eth0 interface goes up. – mosvy Apr 7 at 18:40
  • 1
    I'm assuming that your script will be always started before the eth0 interface is already up; if that's not the case, then you'll have to use a loop as in the other answer, or: while ! ip link show eth0 | grep -q 'state UP'; do sleep 2; done. – mosvy Apr 7 at 19:28
1

Check the status of eth0 from /sys/class/net/eth0/operstate and wait until the network interface to be up:

while ! [ "$(cat /sys/class/net/eth0/operstate)" = "up" ] 
do
    echo  "waiting for eth0 to be up"
    sleep 2
done

ip link add macvlan0 link eth0 type macvlan mode bridge
ip addr add 192.168.0.240/32 dev macvlan0
ip link set macvlan0 up
ip route add 192.168.0.240/28 dev macvlan0
  • 1
    Something's wrong, the network/route is not created at boot up. It seems that the result from the cat is up when this first runs but the ip * commands don't seem to have any effect. – Ricardo Amaral Apr 7 at 17:07
  • 1
    I might have fixed it... I had 2 boot up tasks on my NAS (this one and other related to something else) and I was getting some errors. I made this task be dependent on the other so that it would only run after the other one was finished. It seems to have worked but will do further testing. – Ricardo Amaral Apr 7 at 17:34
1

Here's the solution I eventually solved my problem with:

while ! ip link show eth0 | grep -q 'state UP'; do
    sleep 1
done

ip link add macvlan0 link eth0 type macvlan mode bridge
ip addr add 192.168.0.240/32 dev macvlan0
ip link set macvlan0 up
ip route add 192.168.0.240/28 dev macvlan0

However, I had 2 "boot-up" scheduled tasks on my Synology and I was getting some errors at boot-up. To solve that issue I configure this task to have the other one as pre-task. In other words, the tasks executed in sequence, waiting for the previous task to finish before starting the new one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.