Detailed explanation of Linux--init.d directory

I forgot where I read the article. If I find it, the original address will be attached.

The scripts in the /etc/rc.d/init.d/ directory are similar to the registry in windows, and some specified scripts will be executed when the system starts.

1. Linux boot process

After the system is started, before entering init.d, let's take a look at what the system has done. After the system is powered on, the first hardware self-check is performed, and then the bootloader initializes the system and loads the kernel. After the kernel is loaded into memory, execution begins. Once the kernel is up and running, hardware detection determines which device drivers need to be initialized. From here, the kernel can mount the root filesystem (a process similar to how Windows recognizes and accesses the C drive). After the kernel has mounted the root file system and initialized all device drivers and data structures, it completes the boot process by starting a user-level program called init.

2. Run level

The Init process is the first user process after the system starts, so its pid is always 1. The first thing the init process does is to read the initdefault id value in the inittab file in the /etc/ directory, which is called the run level. It determines what level the system will run at after startup. Runlevels determine most of the behavior and purpose of system startup. This level goes from 0 to 6 and has different functions. The different runlevels are defined as follows:
# 0 – shutdown (never set initdefault to 0 or the system will never start)  
# 1 – single-user mode
# 2 – multi-user, no NFS
# 3 – full multi-user mode (standard runlevel)
# 4 - system reserved
# 5 - X11 (x window)
# 6 - reboot (do not set initdefault to 6, otherwise it will keep rebooting)

3. The relationship between /etc/rc.d/ and /etc/rc.d/init.d

Writing this, it should be almost time to enter init.d, but I don’t think it can be explained clearly if you just write /etc/rc.d/init.d, just use it with /etc/rc.d It may be more appropriate to discuss the first-level directories together, because they are inextricably linked.
Here first explain what is in init.d. This directory stores some scripts, usually the startup scripts of some services set when linux is installed as an rpm package. There are many rpm packages installed in the system, and there are many corresponding scripts. Executing these scripts can be used to start, stop, and restart these services. As mentioned earlier, the scripts in the /etc/rc.d/init.d directory are similar to the registry in Windows and are executed when the system starts. The program runs here (the init process reads the run level). I believe that from the perspective of naming, everyone can guess that the script in /etc/rc.d/init.d should be run, otherwise why is it called init(.d) ) is it. Yes, it is time to run the script in init.d, but not directly, but selectively because the system does not need to start all services. So, how does the system choose which to start and which not to? At this time, the run level just mentioned will come into play.
After determining the run level of system startup, the script /etc/rc.d/rc is executed first. In the source code of RH9 and FC7, it is check_runlevel() as soon as it comes up (although the implemented code is different, it is similar), after knowing the run level, for each run level, there is a subdirectory under rc.d They are rc0.d, rc1.d ..... rc6.d. Under each directory are some links to scripts that are part of the init.d directory. The services to be executed at each level are in the corresponding directory. For example, the services to be started at level 5 are placed under rc5.d, but under this rc5.d are some link files, which are linked to init. The corresponding file in d, the script that actually works in init.d.
At this point, it is estimated that everyone may be more clear, and I initially thought that was the case. However, after I carefully looked and compared these link files with the filenames of the scripts that were actually executed in init.d, there were still several problems that I didn't understand. Taking the opportunity to write this article, I did some homework and finally solved those doubts:
1. Why is there a Kxx or Sxx in front of these linked files? It's like this, with K means to stop (Kill) a service, S means to start (Start) meaning
2, what about the numbers after K and S? What is it for? When I started, I thought it was for arranging to look good or for counting. It was later discovered that it was not. Its role is to sort, that is, to determine the order in which these scripts are executed, the smaller the value is executed first, and the larger the value is executed later. In many cases, the execution order is very important. For example, to start the Apache service, the network interface must be configured first. Otherwise, it would be funny if a machine without IP starts the http service. . .
3. I accidentally found that the same service with S and K is the same script after linking to init.d. I was wondering, why is it executing the same script? At this time, it is really the magic of S and K. It turns out that S and K are not just used to look clearly. S and K also pass the parameters of start and stop to the scripts below init.d respectively. Oh, it's like this (looks enlightened, hehe)! Then I remembered the /etc/rc.d/init.d/network restart command that I had used countless times. It turns out that when passing S, it is equivalent to executing the command /etc/rc.d/init.d/xxx start. Of course, K is equivalent to /etc/rc.d/init.d/xxx stop.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325854674&siteId=291194637