OpenWrt Forum Archive

Topic: How to create script in /etc/init.d/

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

I can't get it to work,

I have to create this script:
#!/bin/ash /etc/rc.common

START=109
STOP=151

start() {
    echo "start swap"
    swapon /dev/sda2
}

stop(){
    echo "stop"
}

My steps as logged in as Root:
Step 1:
root@OpenWrt:~# vi /etc/init.d/swapon

Step 2:
Enter vi mode
Write te script as above

Step 3:
exit vi editing mode

Step 4:
save the script by typing :w

Unfortunately I get the response > 'Error writing.. No such file or directory

So I just created the script, but can't save it.

How come, please help me!

Does the "/etc/init.d" directory exist?

/etc/init.d/ is part of the default directory layout - is your overlay writeable?

Are you wanting this script to start when the router is rebooted?

You can create the script just about anywhere.. Try in the root directory to start. Just follow the steps outlined above from the /root directory.

From there, you will need to make the script executable. Just start with 777 to test.

chomd 777 scriptname.sh

Then vi /etc/rc.local

Edit the rc.local file, and put in the path to the script

/root/scriptname.sh

then save by  :wq! 

Reboot the router, and it will run your script at boot.

The discussion might have continued from here.