How to autorun a shell script after boot

i just got interested in Developing OpenWRT and want to try somethings.

i'm trying to run a script right after my OpenWRT boots. here is my script file

customchain.sh

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

START=99

start(){
[my custom chains]
}

i've placed this script file at /etc/init.d

i wonder why this won't work. if i check at /etc/rc.d, i can see S99customchain.sh -> ../init.d/customchain.sh

when i manually restart script file at root directory with /etc/init.d/customchain.sh i can see it applies well when i check via iptables -L

but as i said, this won't automatically starts when i (re)boot my OpenWRT system.

authority is already given by using +x

i've already tried using /etc/init.d/customchain enable and still doesn't work.

also i already checked similar question at here, and it doesn't have any answers.

what should i do?

there's a spot in the UI where you can define your own custom start script.

in 19.07 it's in System > Startup > Local startup (tab)

if you are telling me to use WEB UI, i'm trying to do this editing scripts inside sdk's files so i can compile in to .bin file

So i can't do what you told me to do

If it is iptables related, why don't you insert your custom rules in /etc/firewall.user?

2 Likes

good point.
it is related with iptables.

i tried insert those commands in to firewall.user

however, when i 'reboot' my device, it doesn't apply.
for this, i'm gonna make a new draft. please go check after about 5 minutes

if i manually run that script file, it works without any errors

start_service() is probably the one (auto startup)
and start() is for manual startup

try and use absolute directories for applications / files
and you can run your application/ script / config via an initscript
if you only want it to run on firstboot after new flash/upgrade or after system restore, you can add a uci_defaults script to your application

same result comes out with start_service()
i tried boot() also.

PATH is not set yet when the script runs. Use the approach below to get the exact error messages.

It probably starts too early in the boot process to insert iptables and/or they may be flushed/overwritten by the firewall/other processes if they start later. I'd try logging from your init script to make sure it runs, if that's the case, you may have to employ a delay in boot() before running start_service. You may want to check the source for adblock or simple-adblock for an example of waiting until network settles after boot.

PS. Not sure if START=99 is a meaningful value. I vaguely remember something about highest recommended value being 95 or even 90. You may want to search github for what other packages are using as a highest number.

i used START's value as 99 because i thought firewall's gonna flush my settings.
However my firewall starts at 30 and i've tried value for START around 40~50 too.

still i should use try using 90, 95

If START=99 is still too early, just insert a "sleep number-of-seconds" line in your startup script and adjust the number-of-seconds as necessary.

I don’t know if the below are exceptions to above, but I see those values used in these startup init scripts on my system.

95       done
96       led
98       sysntpd
99       bootcount
99       urandom_seed

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.