Embedded Linux: How to run a shell script at startup on Hisilicon’s Hi3518?
I am now developing on a Hisilicon’s chip, Hi3518E. The Linux kernal is built and compiled from the code of the SDK provided by Hisilicon. Thus, I think what I am going to say is also valid for other chips from Hisilicon.
I have studied Hi3518 for a few months. I now want to load some drives and programs at startup automatically so I spent some time to figure out how to do it on this chip. First of all, since I needed to run a set of commands, I wrote a Shell Script to organize all the commands together. Thus,I can run them all when I run the script once. Then, put the script into startup. I am not going to talk about how to write a Shell script as I don’t really know much about it.
0. Basic Knowledge
/etc/init.d/ : init.d is a folder for containing Unix/Linux Shell Scripts. If you enabled init.d in your kernal, the system will load and run the scripts placing here when it turns on. In common, all the init scripts of the services needed in a system are placed in this folder. So, you should find it on almost every Linux.
/etc/init.d/rcS : rcS is a script put in init.d folder. It states what Shell scripts will be run next. So, if you placed a script in
/etc/init.d/, it isn’t necessary to run but has to match the rules stated in rcS, in order to be loaded. It is because the system actually loaded
/etc/inittab first, after switching on. After that, run other scripts stated in inittab. If you open inittab, you can see:
# /etc/inittab init(8) configuration for BusyBox # many comments... ::sysinit:/etc/init.d/rcS ::sysinit:/bin/mkdir /dev/shm
The first command is to run
PS. If you open
/etc/init.d/rcS and you will see what is inside the script. I will say more about it later.
Let’s see what is in
/etc/init.d/ on Hi3518E:
1. Put the Shell Scripts into /etc/init.d
cp /your_path/script_name /etc/init.d/
your_path is the path of the Script;
script_name is the name of the Script.
Or, if you know the text editor, vi, you may use it to write a new shell script.
2. Make your scripts executable
chmod +x /etc/init.d/script_name
3. Make the Shell Script run when the system switches on
As I mentioned previously,
/etc/init.d/rcS states the scripts had to be loaded next. Here is what it contains at the end of the file:
#! /bin/sh /bin/mount -a for initscript in /etc/init.d/S[0-9][0-9]* do if [ -x $initscript ] ; then echo "[RCS]: $initscript" $initscript fi done
Thus, we know that the system will only run those scripts named “SnnXXXXX“, where n is any number (0 – 9) and X is any characters, such as S00devs, S01udev, S80network, S99config……
If we need the system to run other scripts automatically, we have to modify rcS. Yet, the simplest way to make it work is to change your scripts’ name and make it satisfy rcS.
mv ./script_name ./S99script_name
Reboot your Linux and you should see that your script can be run automatically once it turns on.
In my os, there are logs llke: (which is actually created by rcS)