init 进程

来源:互联网 发布:彻底删除node和npm 编辑:程序博客网 时间:2024/04/28 17:43
Init 
The kernel, once it is loaded, finds init  in sbin and executes it.When init  starts, it becomes the parent or grandparent 
of all of the processes that start up automa tically on your Linux system. The first thing init  does, is reading its 
initialization file, /etc/inittab. This instructs  init  to read an initial configuration script for the environment, which 
sets the path, starts swapping, checks the file systems, and  so on. Basically, this step takes care of everything that 
your system needs to have done at system initialization: setting the clock, initializing serial ports and so forth. 
Then init  continues to read the /etc/inittab file, which describes how the system should be set up in each run level 
and sets the default  run level . A run level is a configuration of pr ocesses. All UNIX-like systems can be run in 
different process configurations, such as the single user mode , which is referred to as run level 1 or run level S (or 
s). In this mode, only the system administrator can connect  to the system. It is used  to perform maintenance tasks 
without risks of damaging the system or user data. Naturally, in this configuration we don't need to offer user 
services, so they will all be disabled. Another run level is the reboot run level, or run level 6, which shuts down all 
running services according to the appropriate procedures a nd then restarts the system. Commonly, run level 3 is 
configured to be text mode on a Li nux machine, and run level 5 initializes the graphical login and environment. 
After having determined the default run level for your system, init  starts all of the background processes necessary 
for the system to run by looking in the appropriate rc directory for that run level.  init  runs each of the kill scripts 
(their file names start with a K) with a stop parameter. It then  runs all of the start scripts (their file names start with 
an S) in the appropriate run level directory so that all  services and applications are started correctly. In fact, you can 
execute these same scripts manually after the system is finished booting with a command like  /etc/init.d/httpd  stop  
or service httpd  stop  logged in as  root , in this case stopping the web server. None of the scripts that actually start 
and stop the services are located in /etc/rc<x>.d. Rather, all of the files in /etc/rc<x>.d are symbolic links that point 
to the actual scripts located in /etc/init.d. A symbolic link is  nothing more than a file that points to another file, and 
is used in this case because it can be created and deleted without affecti ng the actual scripts that kill or start the 
services. The symbolic links to the various scripts are numbered in a particular order so that they start in that order. 
You can change the order in which the services start up or are killed by changing the name of the symbolic link that 
refers to the script that actually controls the service.  You can use the same number multiple times if you want a 
particular service started or stopped right before or after  another service, as in the example below, listing the content 
of /etc/rc5.d, where crond  and  xfs are both started from a linkname starting with  "S90". In this case, the scripts are 
started in alphabetical order. 
After init  has progressed through the run levels to get to th e default run level, the /etc/inittab script forks a getty 
process for each virtual console (login prompt in text mode).  getty opens tty lines, sets their modes, prints the login 
prompt, gets the user's name, and then initiates a login process for that user. This allows users to authenticate 
themselves to the system and use it. By default, most systems offer 6 virtual consoles, but as you can see from the 
inittab file, this is configurable. 
 
The idea behind operating different services at different run levels essentially revolves around the fact that different 
systems can be used in different ways. Some services cannot be  used until the system is in a particular state, or 
mode, such as being ready for more than one user or having networking available. There are times in which you 
may want to operate the system in a lower mode. Examples are fixing disk corruption problems in run level 1 so no 
other users can possibly be on the system, or leaving a server in run level 3 without an X session running. In these 
cases, running services that depend upon a higher system m ode to function does not make sense because they will 
not work correctly anyway. By already having each service assi gned to start when its particular run level is reached, 
you ensure an orderly start up process, and you can quic kly change the mode of the machine without worrying 
about which services to manually start or stop. Available run levels are generally described in /etc/inittab, which is 

partially shown below:

# # inittab   This file describes how the INIT process should set up #           the system in a certain run-level.  # Default runlevel. The runlevels are: #   0 - halt (Do NOT set initdefault to this) #   1 - Single user mode #   2 - Multiuser, without NFS  #  (The same as 3, if you do not have networking) #   3 - Full multiuser mode #   4 - unused #   5 - X11 #   6 - reboot (Do NOT set initdefault to this) 



原创粉丝点击