Linux init. d

Source: Internet
Author: User

Reproduced from: http://blog.chinaunix.net/space.php? Uid = 20422917 & Do = Blog & id = 1682874

Auxiliary environment: rh9, fc7
In addition, if there is no special explanation in this article, init. d refers to the/etc/rc. d/init. d directory. Reprinted

This article includes three parts
1. Linux boot process
2. Running level
3. Relationship between/etc/rc. d/and/etc/rc. d/init. d
All of them are limited to your own understanding. Please correct and supplement any errors or deficiencies! Learn together and make progress together.

"The scripts in the/etc/rc. d/init. d/directory are similar to those in the Windows registry. Some specified scripts will be executed when the system starts ". Before that, I would like to cite an understanding of Manager Li shanming's summary last night to give you a rough impression on the init. d directory. Before going to init. d, let's make two preparations, the Linux boot process and the concept of running level.

I. Linux boot process

After the system is started, before entering init. d, let's first take a look at what the system has done. First, let's look at a picture (this picture comes from the network, and it will be added later)
From this figure, we can look at the entire process of starting to guide from a relatively high perspective, which is clear and clear. After the system is powered on, the hardware self-check is performed first, and then the bootloader initializes the system and loads the kernel.
After the kernel is loaded into the memory, the execution starts. Once the kernel starts running, the hardware detection determines which device drivers need to be initialized. From here on, the kernel can mount the root file system (this process is similar to Windows's process of identifying and accessing the C drive ). After the kernel is mounted to the root file system and all device drivers and data structures have been initialized, a user-level program called init is started to complete the boot process.

2. Run level)

The INIT process is the first user process after the system starts, so its PID (process number) is always 1. The first thing to do in the INIT process is to read the initdefault id value in the inittab file under the/etc/directory. This value is called Run-level ). It determines the level at which the system runs after startup. The running level determines most of the behaviors and objectives of system startup. This level ranges from 0 to 6 and has different functions. Different runtime levels are defined as follows:

#0-stop (do not set initdefault to 0, otherwise the system will never start)
#1-single-user mode
#2-multiple users without NFS
#3-full multi-user mode (Standard Operation Level)
#4-reserved by the System
#5-X11 (X Window)
#6-Restart (do not set initdefault to 6, otherwise it will be restarted all the time)
This is a common running level for two tables (supplemented later). Level 3 of redhat9 is started on the text interface, and level 5 of fc7 is started on the graphic interface.

Iii. Relationship between/etc/rc. d/and/etc/rc. d/init. d

Written here, it should be about to enter init. d, but I think single write/etc/rc. d/init. d and/etc/rc. d. It may be more appropriate to discuss the above-level directories because they are closely related.
Here, I will explain what is put in init. d. This directory stores some scripts, which are generally the startup scripts of some services set during the installation of RPM packages in Linux. The system has installed many rpm packages, and there are many corresponding scripts. Run these scripts to start, stop, and restart these services.
As mentioned above, the script in the directory/etc/rc. d/init. d is similar to the Registry in windows and executed when the system is started. The program runs here (the INIT process reads the running level). From the perspective of naming, you can also guess how to run/etc/rc. d/init. d. Why is it also called Init (. d) Right. Yes, it is time to run the script in init. d, but it is not a direct operation, but an alternative because the system does not need to start all services.
Then, how does the system choose what needs to be started and what does not? At this time, the running level just mentioned takes effect. After the run level of the system is determined, the script/etc/rc. d/RC is executed first. In the source code of rh9 and fc7, It is check_runlevel () (although the implementation code is different, it is also similar). After knowing the running level, for each running level, it is in RC. there is a subdirectory under D: rc0.d, rc1.d ..... Rc6.d. Each directory contains links to some scripts in the init. d directory. The services to be executed at each level are under the corresponding directory. For example, the services to be started at level 5 are placed under rc5.d, but some link files are put under rc5.d, link to init. d. d.

Redhat9 ls
Fc7 LS-l
This is clear.
At this point, it may be clear to everyone. I thought it was like this. However, after carefully reading and comparing these link files and the file names of the scripts actually executed in init. d, I still haven't figured out a few issues. With the opportunity to write this article, I have done some homework and finally solved the doubts.
1. Why does kxx or sxx appear before these linked files?
Yes, K indicates that a service is stopped (kill), and s indicates that start is enabled.
2. What are the numbers behind K and S? What is it?
At the beginning, I thought it was a good arrangement or a good number. Later I found that it was not. It is used to sort the scripts, that is, to determine the execution sequence of these scripts. If the values are small, the scripts are executed first, and the scripts are executed after the values are large. In many cases, the execution sequence is very important. For example, to start the apache service, you must first configure the network interface. Otherwise, it would be funny to start the HTTP service with a machine without an IP address...
3. I accidentally found that the same service with S and link with K to init. d is the same script. I wondered why the same script was executed?
At this time, S and K are really amazing. In the past, S and K are not just used to look clearly divided. S and K respectively pass the start and stop parameters to the script under init. d. Oh, that's it )! Then I remembered the countless/etc/rc. d/init. d/network restart commands that I used. When S is passed, it is equivalent to executing/etc/rc. d/init. d/xxx start command. k is equivalent to/etc/rc. d/init. d/xxx stopped

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.