Raspberry Pi_Eng_20.3.5 Automatic Run regardless of Run Level at Booting

20.3.5 Automatic Run regardless of Run Level at Booting

If you want the command or script to always run regardless of run level, you register it in the “/etc/rc.local” file. The script registered here is executed only in run level “2” ~ “5” which is a normal operation status of the system.

Here is an example for the contents of the file “/etc/rc.local”.

Raspberry Pi_Eng_20.3.4 Automatic Execution by Run Level at Booting

20.3.4 Automatic Execution by Run Level at Booting

20.3.4.1 Principles of Registering Init Script

After you have created an init script and saved it in “/etc/init.d”, you need to register it on the system so that it runs automatically according to the run level. If we systematically look into registering on the system, you can see that it is creating a link to the original script file. In other words, running the link will run the original script file.

Raspberry Pi_Eng_20.3.3 Creating Init Script

20.3.3 Creating Init Script

20.3.3.1 Meaning of Init Script

The script that is used when the system boots or shuts down is called “init script”. All init scripts basically should be put under “/etc/init.d/” directory.

There are several types of init scripts as follows according to their usage:

Raspberry Pi_Eng_20.3.2 Booting Sequence

20.3.2 Booting Sequence

When the system first starts up, it executes certain defined tasks in a certain order. This sequence is called “booting sequence”. Next, we will explain the basic framework of booting sequence and how to use it to automatically execute the necessary tasks in booting.

20.3.2.1 Booting Framework

Raspberry Pi_Eng_20.3.1 Run Level

20.3 Automatic Run with Init Script

20.3.1 Run Level

20.3.1.1 Definition of Run Level

On Linux, you need to process all pre-specified tasks when the system first starts or shuts down, or sometimes need to prevent others from using it when performing system recovery tasks. A tool called “run level” is used to limit who can use the system in a certain step or to identify the types of commands or programs that can run in a particular environment.

Raspberry Pi_Eng_20.2.6 Running Script

20.2.6 Running Script

20.2.6.1 Preparing to Run Script File

A script is not a binary executable like a normal executable program, but it must be executable as a program itself. Therefore, the execute permission must be granted to the script file. You can use “chmod” command to grant the desired permission. For the permissions of a file, see the description of [13.2 Changing Permission on File].

Raspberry Pi_Eng_20.2.5 Execution Control of Script

20.2.5 Execution Control of Script

20.2.5.1 Optional Processing according to Conditions

It is “if” statement and “case” statement to be used to selectively process commands depending on a certain condition.

● “if” statement

Raspberry Pi_Eng_20.2.4 Operations in Script

20.2.4 Operations in Script

20.2.4.1 Assignment Operation

When we define a new variable or we assign a value to a variable in a script, it is called an assignment. The operator used in this case is “=”. This is called assignment mode, and the basic format is as follows.

Raspberry Pi_Eng_20.2.3 Basics of Script Syntax

20.2.3 Basics of Script Syntax

In this section, we will describe various rules and syntax for writing scripts.

20.2.3.1 Definition of Script

Script is basically a text file. Internally, the file is defined as a script, and is differentiated from a normal text file. Defining that the file is the script is the value you enter at the beginning of the line as follows.

Raspberry Pi_Eng_20.2.2 How Shell Runs Commands

20.2.2 How Shell Runs Commands

20.2.2.1 Types of Kernel’s System Calls

● “fork” system call

This call is one of the system call methods that a process uses to execute another process. When a command needs to be executed, a child process is created and executed separately.