OpenWrt Forum Archive

Topic: run some command after startup

The content of this topic has been archived on 1 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I'm trying to run some command during startup, so I put this in /etc/rc.local

ifconfig eth0.6 10.10.99.1 up

exit 0

The problem is that when it tries to execute at that moment eth0.6 is still not created, so I'm getting the error message. Is there a way to execute ifconfig a bit later when eth0.6 is ready or when openwrt is fully fully booted?

Thanks

if you look at the network init script you can see that is starts at 20 and ends at 90 ....

#!/bin/sh /etc/rc.common

START=20
STOP=90

SERVICE_DAEMONIZE=1
SERVICE_WRITE_PID=1

start() {
        stop
        [ -e /proc/sys/kernel/core_pattern ] && {
                ulimit -c unlimited
                echo '/tmp/%e.%p.%s.%t.core' > /proc/sys/kernel/core_pattern
        }
        service_start /sbin/netifd

        setup_switch() { return 0; }

        include /lib/network
        setup_switch

        sleep 5

        /sbin/wifi down
        /sbin/wifi up
}

restart() {
        ifdown -a
        sleep 1
        start
}

shutdown() {
        ifdown -a
        stop
}

stop() {
        service_stop /sbin/netifd
}

reload() {
        ubus call network reload
        /sbin/wifi down
        /sbin/wifi up
}

so if you create an init script that starts after that ..

/etc/init.d/my_network

#!/bin/sh /etc/rc.common

START=95

start(){
           ifconfig eth0.6 up
           logger "ETH0.6 IS UP"
           exit 0
}

restart(){
              logger "RESTARTING ETH0.6 ..."
              ifconfig eth0.6 down
              logger "ETH0.6 IS DOWN"
              sleep 1
              ifconfig eth0.6 up
              logger "ETH0.6 IS UP"
              exit 0
}

stop(){
           ifconfig eth0.6 down           
           logger "ETH0.6 IS DOWN" 
           exit 0
}

if you want it to run during boot then you need to enable it

/etc/init.d/my_network enable


you can use the restart and stop options manually like this ..

/etc/init.d/my_network restart

and

/etc/init.d/my_network stop

Many thanks for so in depth answer.

For some reason it is still executed too early. Here is the log:

...
Wed Jan 13 12:24:23 2016 user.notice : ifconfig: SIOCSIFADDR: No such device
Wed Jan 13 12:24:23 2016 user.notice root: ETH0.6 IS UP
...
...
Wed Jan 13 12:24:28 2016 daemon.notice netifd: VLAN 'eth0.1' link is up
Wed Jan 13 12:24:28 2016 daemon.notice netifd: VLAN 'eth0.6' link is up
...

I tried changing START values from 95 to 120 and even to 200 but both times it was logged under the same position (too early). Is there any other way to change the loading priority?

Or I will try even more extreme START values like 1000...

Might adding this to your init.d script help?

boot () {
    sleep 60
    start_service
}

The script I'm using is set with:

START=99
USE_PROCD=1


and it seems to work.

visata wrote:

I tried changing START values from 95 to 120 and even to 200 but both times it was logged under the same position (too early). Is there any other way to change the loading priority?

Or I will try even more extreme START values like 1000...

99 is the last possible priority for startup scripts.
In alphabetical evaluation of filenames, 1000 comes first, then 120, then 200 and finally 95... 

And "starts at 20 and ends at 90" means just that when router is being shut down, the script is run at position 90. (It is not running from 20 to 90 at boot...)

https://wiki.openwrt.org/doc/techref/process.boot#init

hnyman wrote:
visata wrote:

I tried changing START values from 95 to 120 and even to 200 but both times it was logged under the same position (too early). Is there any other way to change the loading priority?

Or I will try even more extreme START values like 1000...

99 is the last possible priority for startup scripts.
In alphabetical evaluation of filenames, 1000 comes first, then 120, then 200 and finally 95... 

And "starts at 20 and ends at 90" means just that when router is being shut down, the script is run at position 90. (It is not running from 20 to 90 at boot...)

https://wiki.openwrt.org/doc/techref/process.boot#init

I've just set to 99, rebooted the router and same issue:

Wed Jan 13 23:46:31 2016 user.notice : ifconfig: SIOCSIFADDR: No such device
Wed Jan 13 23:46:31 2016 user.notice root: ETH0.6 IS UP
...
Wed Jan 13 23:46:36 2016 daemon.notice netifd: VLAN 'eth0.1' link is up
Wed Jan 13 23:46:36 2016 daemon.notice netifd: VLAN 'eth0.6' link is up

According to wiki, network is being launched here?

S40network    start a network subsystem (run /sbin/netifd, up interfaces and wifi

Here is my script which I use:

root@OpenWrt:~# cat /etc/init.d/my_network
#!/bin/sh /etc/rc.common

START=99

start(){
           ifconfig eth0.6 10.10.99.1 up
           logger "ETH0.6 IS UP"
           exit 0
}

restart(){
              logger "RESTARTING ETH0.6 ..."
              ifconfig eth0.6 10.10.99.1 down
              logger "ETH0.6 IS DOWN"
              sleep 1
              ifconfig eth0.6 10.10.99.1 up
              logger "ETH0.6 IS UP"
              exit 0
}

stop(){
           ifconfig eth0.6 10.10.99.1 down
           logger "ETH0.6 IS DOWN"
           exit 0
}
stangri wrote:

Might adding this to your init.d script help?

boot () {
    sleep 60
    start_service
}

The script I'm using is set with:

START=99
USE_PROCD=1


and it seems to work.

I tried adding your suggested code to my existing script but it wasn't executed at all. I dig around a bit and according to this page https://wiki.openwrt.org/inbox/procd-init-scripts, I need to completely rewrite my script.

Do you need it as an init script? Just put sleep 60 in /etc/rc.local:

# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

sleep 60
ifconfig eth0.6 10.10.99.1 up

exit 0

Unless you need to reverse/change this on shutdown, I see no need for an init script.

Simply putting sleep 60 in /etc/rc.local solved my issue!

The discussion might have continued from here.