Sunday, October 25, 2015

Creating Script to Run on Startup for OpenWRT (Barrier Breaker) - kerem izzet atam

Today I wanted to create some script to mount my USB disk to my OpenWRT device on start up and run some script in it.  You might also need it if you have some huge binary to run on boot which is stored in your USB plugged in your OpenWRT device.

There is quite good source about it : http://wiki.openwrt.org/doc/techref/initscripts
But if you are even lazy to read it, here is what work for me.

1- Create some file under /etc/init.d/ I have named mine as  "usb-script.sh"

2- I have put my commands (in red)  under start function in my usb-script.sh file :

#!/bin/sh /etc/rc.common
START=100
start() {
mkdir /mnt/usb
mount /dev/sda1 /mnt/usb/
./mnt/usb/my-script-to-save-the-world.sh
}

START parameter defines the run order of your script among other init scripts. I have defined relatively bigger number (100) to the other scripts to make it safer (at least i hope that way). Check the source i gave above for further reading about it. You can simply look START values of other scripts with the command below :
root@OpenWrt:/# grep START= /etc/init.d/*

And you will likely to see output like this (You can see my usb-script.sh at undermost) :
/etc/init.d/boot:START=10
/etc/init.d/cron:START=50
/etc/init.d/dnsmasq:START=60
/etc/init.d/done:START=95
/etc/init.d/dropbear:START=50
/etc/init.d/firewall:START=19
/etc/init.d/led:START=96
/etc/init.d/log:START=12
/etc/init.d/network:START=20
/etc/init.d/odhcpd:START=35
/etc/init.d/openflow:START=43
/etc/init.d/openvswitch:START=15
/etc/init.d/sysctl:START=11
/etc/init.d/sysfixtime:START=00
/etc/init.d/sysntpd:START=98
/etc/init.d/system:START=10
/etc/init.d/telnet:START=50
/etc/init.d/uhttpd:START=50
/etc/init.d/usb-script.sh:START=100

3- Give execute permission to your script :
root@OpenWrt:/# chmod +x /etc/init.d/usb-script.sh

4- Run command below. It will create simlink in /etc/rc.d directory so it makes your script run on boot :
root@OpenWrt:/# /etc/init.d/usb-script.sh enable

Yeah, that is it. Hope it works.

2 comments:

Anonymous said...

kerem, how can I communicate with you, I am dragon
I sent an email for you, but you didn't check it,
are you using your email address now, right?

last time when I visited turkey my english speaking skill was not fluent to talk with but now I can speak what I want to say fluently. and you can remember that I will visit turkey again, 3 years later, I told you, and year 2016 is at that time.

please check your email or inform me how can I get along with you.

Qqwy said...

Hi there! Thank you for your post. It helped me get started with writing my own init script.

I do want to point out, to whomever it may concern, that because init scripts turn out to be ordered alphabetically, using a START number higher than 99 (or lower than 10) will not have the effect you expect. In the case of your example, your code will already run after any script with START 10.

Have a wonderful day :-)