OpenWrt Forum Archive

Topic: Adding Custom Scripts in init.d with Custom Build

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

Hi,

I am building a Alix2 image with AA and would like to know how to include my own custom scripts in the init.d directory so that it would run on boot.   The script here is using calls to setup iptables and tc (so I am not building any custom packages).   

Why I need this?  Well I want to create a standard build so that I can replicate to quite a number of units to be distributed to branch offices. 

Thanks for helping.

To my knowledge init.d scripts are not run at startup time unless you eed add a symlink to /etc/rc.s:
http://wiki.openwrt.org/doc/techref/process.boot#init
executes the symlinks to the actual startup scripts located in /etc/rc.d/S##xxxxxx with option "start"


Alternative might be to write the commands in to /etc/rc.local , which is executed at the end of the boot process.
That is the preferred place for user commands.
http://wiki.openwrt.org/doc/howto/notuc … tcrc.local

(Last edited by hnyman on 28 Apr 2014, 12:47)

Hi,

I tried the following and it works.  Place the script that you want to run in <build root>/files/etc/init.d directory and make it executable (chmod 755).   

Build the image. One the first boot the system will create the symlink and enable the script but you have to make sure that the script is make executable when it is place in the directory before the build.  Hope this helps.

Yes, but you need to enable it after firstboot. I am wondering how to make the script enabled by default, i.e., after you build a image, it is already enabled and no need to enable it manually.

stone8936 wrote:

Yes, but you need to enable it after firstboot. I am wondering how to make the script enabled by default, i.e., after you build a image, it is already enabled and no need to enable it manually.

As long as you make sure that when you place the script in the <build root>/files/init.d/ directory it is make executable (eg chmod 755 on the script).   

Once you compile, load the image and then on the firstboot the system will take care of making it enabled.   You as the user do not need to do anything further.  Hope this helps.

stone8936 wrote:

Yes, but you need to enable it after firstboot. I am wondering how to make the script enabled by default, i.e., after you build a image, it is already enabled and no need to enable it manually.

You can do this via uci-defaults.The scripts in the uci-defaults directory are running once after flashing the firmware. So, put the enable command there to enable your init script in the final image. E. g.:

files/etc/uci-defaults/myscript:

#!/bin/sh

/etc/init.d/myscript enabled && {
    /etc/init.d/myscript stop
    /etc/init.d/myscript disable
}

exit 0
chmod 0755 files/etc/uci-defaults/myscript

Should work!

I Placed the script in <build root>/files/etc/init.d directory and make it executable (chmod 755).   
Build the image. but script is not working in my Router

ac@wrt wrote:

I Placed the script in <build root>/files/etc/init.d directory and make it executable (chmod 755).   
Build the image. but script is not working in my Router


You need to enable the script as described in this thread.

/etc/init.d/scriptname enable

If it still doesn't work you probably have a syntax error

Thanks...following is my sample script., any problem in syntax ?

/etc/init.d$ vi testlog

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

START=00

boot() {
        /etc/init.d/testlog enable
        uci set system.@system[0].hostname=NewHostName
        uci commit system

}

ac@wrt wrote:

Thanks...following is my sample script., any problem in syntax ?

/etc/init.d$ vi testlog

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

START=00

boot() {
        /etc/init.d/testlog enable
        uci set system.@system[0].hostname=NewHostName
        uci commit system

}

Yes, many problems.

Firstly, as I told you previously in another thread, you set the hostname by editing the file /etc/config/system and changing the hostname option to your desired hostname. You can also change the hostname by using Luci. Go to Luci --> System --> System and change the value in the Hostname field. Using an init.d script to make this change is the WRONG way to go about doing it.

Secondly, your script should implement a start(), stop() and restart() method. NOT a boot() method.

Thirdly, you are recursively calling your own script in the first line. You enable an init.d script ONCE from the command line by invoking it with the command /etc/init.d/scriptname enable. You do not embed this call within the script itself.

Fourthly, you cannot start your script with a 00 number. The system script is executed with priority 10. This script sets the hostname. If you try to execute your init.d script before the system script, then the system script will undo your changes. Your script needs to be numbered AFTER the system script. So START should have a value > 10....

Really - I suggest taking a little more time to read the answers you're given on this forum and also browse through the openwrt wiki. It has a lot of information that is very helpful.

(Last edited by dl12345 on 16 Nov 2016, 21:33)

The discussion might have continued from here.