Linux initialization process init analysis __linux

Source: Internet
Author: User

1. Init process

The init process, a user-level process initiated by the kernel, completes the boot process by launching Init when the kernel starts itself. Therefore, Init is always the first process (its process number is always 1). The INIT program is located in/sbin/init, and if the kernel cannot find Init, it will try to run/bin/sh;

2. Operation Level

The run level is the level of functionality that the operating system is currently running. This level, from 1 to 6, has different functions. 0– downtime (never set Initdefault to 0) Single user mode 2– multiuser, no NFS 3– full multiuser mode (standard run level) 4– no 5–x11 (Xwindow) 6– reboot (Don't take Init Default is set to 6)

3, Inittab documents

The Linux system configures the run level in the/etc/inittab file for the Init process to invoke. By default, the scripts that are in the/ETC/RC.D/INIT.D directory are started, which are connected to the/ETC/RC.D/RCN.D directory by the "ln" command (start n represents the run level of 0-6). The Inittab file contains fields that have strict formatting. The format of each entry in the file is:

Id:rstart:action:process:

Where the ID field is the unique identifier of the corresponding process. The Rstart field contains a number that indicates the level at which the process is running. The Action field tells the INIT process how to treat processes that correspond to the process. There can be many kinds of action, but the most common is wait and respawn,wait means that when the process starts and waits for it to end, respawn means that if the process does not exist, start the process, and if it exists, restart it immediately as soon as it falls. The process field contains the actual commands to run.

4, RCN directory

The scripts in the RCN.D directory are links to save unnecessary copies. The formats for these links are:

Snnn.script_name
Knnn.script_name

which
S: Representative initiates the corresponding process, K: represents killing the corresponding process. NN: is a 00 to 99 two-digit number, but in some systems it is 000 to 9,993 digits. The same number is used for links in different directories. For example, if a service is called s45.myscript when it is started in rc3.d, you should use the link name S45.myscript if you want it to start in rc2.d. Script_name: The file name of the corresponding script, depending on the operating system, may be located in the following directory:/USR/SBIN/INIT.D,/ETC/RC.D,/etc/init.d.
When the init process invokes the corresponding run-level script, the kill process follows the K-sequence from highest to lowest, that is, the K23.myscript-> k12.named, and the START process follows the sequence number from lowest to highest.

5. Custom Start Service

The Create startup service can be implemented in the following 3 ways:

1). Create Startup Service Script

Creating a startup script must meet the following criteria: Write the script to make sure it conforms to the calling criteria. Be sure that it can start or terminate the corresponding service. Place the script (depending on the operating system)/ETC/INIT.D or/USR/SBIN/INIT.D or/ETC/RC.D. Create a link in the appropriate RCN.D directory in a reasonably named way.

For example, you will now start and stop a service called Rc.audit, which runs at level 3 and stops at level 6. By looking at the entries in RCN.D, we found that the ordinal 35 was idle, so we used that ordinal (in fact, the system does not check for the use of the occupied ordinal number).

Assuming that the script has passed the test, it can start and stop correctly. Now let's put the script in the appropriate run level directory.
In this system, the RCN.D directory is located in the/etc/rc.d directory, and my run level script is kept under the/ETC/RC.D/INIT.D directory (if the system directory structure is different from the above, the following commands need to be adjusted accordingly).

$pwd
/etc/rc.d/rc3.d

$LN-S/init.d/rc.audit s35rc.audit
$ls-L
Lrwxrwxrws 1 root root s35rc.audit->/init.d/rc.audit

$pwd
/etc/rc.d/rc6.d

$LN-S/init.d/rc.audit k35rc.audit
$ls-L
lrwxrwxrwx 1 root root k35rc.audit->/init.d/rc.audit

2). Inittab Start

We can also do this by adding a corresponding entry in the Inittab file. First I'm sure that the script will be able to run a backup of the Inittab file correctly. $ cp/etc/initab/etc/inittab.bak Edit Inittab file, add an entry at the end of the file: Rc.audit:3:once:/usr/local/etc/rc.audit, save and exit.

One of the above means:
The rc.audit of the beginning of the line is the unique identification of the process at run Level 3, which runs only once and the script to run is/usr/local/etc/rc.audit.

3). rc.local Start

If you do not want to make/etc/inittab files too cluttered, there are other ways to implement the ability to start and stop services. Most systems contain a file called rc.local, which is generally located in the/etc directory. The script file will run after the Inittab and run level scripts. You can include any command in the file, or invoke the most used startup script from it. Some systems also contain a script file named shutdown in the/bin directory (more in the/usr/sbin directory), which can be used to turn off certain services.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.