The concept of running level comes from System V. The running level divides the startup process into different sets. Each set contains a set of scripts. When the init program "switches the running level" to the corresponding level, the corresponding script is triggered. You can execute the init [level number] to switch to the running level. (For example, in Linux, run level 6 indicates reboot, so executing init 6 will cause the System to restart.) The runtime-level Definition of each System V is not exactly the same, it can only be determined by Directly Reading/etc/inittab. Below is a standard Linux/etc/inittab. Note that the content described here comes from redhat, but other versions are similar. We divide it into several sections for explanation. As in general, the line starting with # Is a comment, and the syntax of a line not a comment is: Label: Run level: Operation Method: the command label is the label of this line. The run level is used to define the level that this line should be used for. If it is null, it is defined as all levels. The operation method can be some definite strings, indicates how to execute the following command, and the command is for the program that is executed at this level.Below is its content: # Default runlevel. the runlevels used by RHS 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) # The above lines explain the default Operation Level Definition: shutdown, the system shuts down after entering this level; in single-user mode, the system can only operate on the computer from the console, the network and terminal are not started, and many file systems are not connected. In multi-user mode, however, the network service is disabled to support full multi-user mode, which is not defined in the general mode. Graphical interface mode. The system automatically starts X Window after this operation level. The definition of restarting these levels is arbitrary. However, you 'd better not modify it, especially level 1, 1, and 6, because many programs use methods such as init 0 to control the system, other Linux releases may change the definition of 2-5, you need to refer to/etc/inittab to determine the meaning of the level. However, in general, and 6 are always defined above. The following is the real content. First, the system must define the default running level: id: 3: initdefault. The keyword determines the default running level. Here is 3, that is, after the public System startup script is executed, the System will execute the lines corresponding to level 3 # System initialization. si: sysinit:/etc/rc. d/rc. the sysinit keyword in sysinit defines the public "system initialization" script. It is null at the corresponding running level, indicating that it applies to all running levels. Note that it will be completed before the system enters any running level. We will study the content of this script later. L0: 0: wait:/etc/rc. d/rc 0l1: 1: wait:/etc/rc. d/rc 1l2: 2: wait:/etc/rc. d/rc 2l3: 3: wait:/etc/rc. d/rc 3l4: 4: wait:/etc/rc. d/rc 4l5: 5: wait:/etc/rc. d/rc 5l6: 6: wait:/etc/rc. d/rc 6: Six runtime-level definitions are provided here. If the runtime-level 0 is used, run the command/etc/rc. d/rc 0, run level 1 is/etc/rc. d/rc 1 ,..... and so on. The wait keyword indicates that the system must wait until the command is executed to start the next step. # Things to run in every runlevel. ud: once:/sbin/update this is another command for all levels. The update command starts the updated daemon to regularly refresh the super block table in the memory. The Once keyword indicates that this item is executed only Once. # Trap CTRL-ALT-DELETEca: ctrlaltdel:/sbin/shutdown-t3-r nowctrlaltdel defines the system behavior when the hot start key combination is triggered, all running-level responses to it are defined as shutdown-r restart.) pf: powerfail:/sbin/shutdown-f-h + 2 "Power Failure; system Shutting Down "pr: 12345: powerokwait:/sbin/shutdown-c" Power Restored; Shutdown Cancelled "define how to respond to ups information if the System powers Down (powerfail ), execute the command to shut down after two minutes. If the power is restored before the shutdown, cancel the shutdown operation. # Run gettys in standard runlevels1: 2345: respawn:/sbin/mingetty tty12: 2345: respawn:/sbin/mingetty tty23: 2345: Weight:/sbin/mingetty tty34: 2345: respawn:/sbin/mingetty tty45: 2345: respawn:/sbin/mingetty tty56: 2345: respawn:/sbin/mingetty tty6 2, 3, and 5 are multiple users, if six virtual screens are opened for the system, Alt + Fn is used to obtain the virtual screen.) The respawn keyword indicates that this action is executed every time it enters the corresponding running level. # Run xdm in runlevel 5 # xdm is now a separate servicex: 5: respawn:/etc/X11/preofdm-nodaemon for Level 5, start the GUI. To sum up, what will the system do after reading the inittab? Set Ctrl + Alt + Del response, set UPS support, and then execute/etc/rc. d/rc. sysinit, And then/etc/rc. d/rc 3, and then update and start the virtual screen. Obviously, the main initialization commands of the system should be completed in/etc/rc. d/rc. sysinit and/etc/rc. d/rc 3.BASIC script under rc. dNext we will study the startup script. The script here comes from redhat 6.1, because it is the most widely used version, and the special issues of other versions will be discussed in the next section. First, the system will execute/etc/rc. d/rc. sysinit: This is a shell script. You can use a common text editing tool to process it. For simplicity, we only study the more important part or typical section :#! /Bin/sh #/etc/rc. d/rc. sysinit-run once at boot time # Taken in part from Miquel van Smoorenburg's bcheckrc. # Rerun ourselves through initlogif [-z "$ IN_INITLOG"]; then [-f/sbin/initlog] & exec/sbin/initlog $ INITLOG_ARGS-r/etc/rc. d/rc. sysinitfi first, determine whether the/sbin/initlog file exists in the system. If yes, record the initialization information. # Set the pathPATH =/bin:/sbin:/usr/bin:/usr/sbinexport PATH to Set the default PATH. # Read in config data. if [-f/etc/sysconfig/network]; then. /etc/sysconfig/networkelseNETWORKING = noHOSTNAME = localhostfi is the network parameter settings. The content of/etc/sysconfig/network is as follows: NETWORKING = yesFORWARD_IPV4 = "yes" HOSTNAME = "openlab.asnc.edu.cn" GATEWAY = "" GATEWAYDEV = "" obviously, if this file exists, SET network running parameters, such as domain names and gateways, this file can contain many things. For more information, see # Source functions. /etc/rc. d/init. d/functions/etc/rc. d/init. d is where all service scripts are stored, and functions is the parameter settings required by various service scripts. If you are interested, you can take a look. The following section shows how to set some display information, and the following content: # Mount/proc (done here so volume labels can work with fsck) action "Mounting proc filesystem" mount-n-t proc/proc link/proc file system. Remember that/proc is a virtual file system used to display the system status, pay attention to the syntax of the action Command. It displays a prompt and then executes the corresponding command. Then the paragraph has the following meanings: # Turn off sysrq # if ["$ MAGIC_SYSRQ" = "no"]; then # echo "0">/proc/sys/kernel/sysrq # fi $ MAGIC_SYSRQ = no means you decide not to use kernel debugging. The script must disable the system's kernel debugging function, pay attention to the following processing method: create a file named sysrq under/proc/sys/kernel and set its content to "0". This function is disabled, this is also a standard method to enable or disable a function of the kernel during running. We will often see this example later. Next we need to set the clock and keyboard ing table, load the system font, and it is a lengthy piece of code, which is omitted here. You can always see them on your system. # Start up swapping. action "Activating swap partitions" swapon-aswapon-a reads the/etc/fstab file, which contains a list of various file systems that should be automatically attached to the system, it also contains knowledge about swap partitions. swapon-a will start all swap partitions marked in it. # Set the hostname. action "Setting hostname $ {HOSTNAME}" hostname $ {HOSTNAME} # Set the NIS domain nameif [-n "$ NISDOMAIN"]; thenaction "Setting NIS domain name $ NISDOMAIN" domainname $ NISDOMAINelsedomainname "" fi sets the system name. We should remember that $ HOSTNAME has been set in the/etc/sysconfig/network file, therefore, the action is executed, while $ NISDOMAIN is now a Null String. Therefore, hostname is set after execution, but the NIS domain name does not exist. If [-f/fsckoptions]; then fsckoptions = 'cat/fsckoptions' else fsckoptions = fiif [-f/forcefsck]; then fsckoptions = "-f $ fsckoptions" fi this is an administrator-related line. If the/directory of the system contains a/forcefsck file, the system automatically starts the fsck program to check whether the file system has an error. Next, let's take a look at whether or not the/fastboot file exists. The system will decide whether to enable PNP. The method is similar to MAGIC_SYSRQ. We will not discuss these two sections, you can take a look. # Remount the root filesystem read-write.action "Remounting root filesystem in read-write mode" mount-n-o remount, rw/# Add/proc to/etc/mtabmount-f-t proc/proc after detecting the root file system, the system reconnects/to the read/write mode, add/proc to/etc/mtab. The following is an important step. If the system kernel supports the module to be loaded, you need to set the $ USEMODULES variable to "y" and set the default path and parameters of the module. Then, the system starts to load the module: # load sound modulesif [-n "$ USEMODULES"]; then if grep-s-q "^ alias sound"/etc/conf. modules; thenaction "Loading sound module" modprobe sound fi ......... fi ........... after these directly loaded modules are completed, this script tries to find/etc/rc in order to be compatible with the previous method and to facilitate the administrator. d/rc. modules: # Load modules (for backward compatibility with VARs) if [-f/etc/rc. d/rc. mod Ules]; then/etc/rc. d/rc. modulesfi obviously, you can add your own module initialization command to/etc/rc. d/rc. modules enables it to run at startup. After loading the main modules, the system starts a series of daily work, such as detecting problematic file systems, linking all local file systems, and starting the disk quota. If the system has not been configured, the configuration script will be started to set the network and Super User Password. Otherwise, for the configured system, clear the accounting files and prepare for system work. After everything is completed, the system enters runtime Level 3 according to the inittab settings and runs/etc/rc. d/rc 3. /Etc/rc. d/rc is a very interesting program. It is a shell script and its behavior is as follows: based on the parameters provided to it, it searches for the corresponding directory rc ${arg1 }. d, for example, in/etc/rc. d/rc 3 queries/etc/rc during execution. all the files in d/rc3.d are trying to execute all the scripts that start with S or K. All the scripts that start with S are added with a start parameter, add a stop parameter to all the scripts that use K to start. The execution order is the numerical order following S or K. For example, in the current example, A S50inet script exists in/etc/rc. d/rc 3, so the rc Script executes S50inet start. The S50inet actually goes to/etc/rc. d/init. A symbolic link of d/inet. Its content is (only part of content is provided here ):. /etc/rc. d/init. d/functions. /etc/sysconfig/networkif [$ {NETWORKING} = "no"] then exit 0fi [-f/usr/sbin/inetd] | exit 0 RETVAL = 0 # See how we were called. case "$1" instart) echo-n "Starting INET services:" daemon inetd RETVAL =$? Echo [$ RETVAL-eq 0] & touch/var/lock/subsys/inet; stop) echo-n "Stopping INET services:" killproc inetd RETVAL =$? Echo [$ RETVAL-eq 0] & rm-f/var/lock/subsys/inet; *) echo "Usage: inet {start | stop | status | restart | reload} "exit 1 esacexit $ REVALdaemon and killproc are in/etc/rc. d/init. the function defined in d/functions. daemon runs the command as a daemon and killproc kills the corresponding process. Obviously, the result of S50inet start is that the inetd program is started as a daemon process. The method here is to start the standard mode of the service process. For example, if you want to set a service to be started at runlevel 3, you can write a script by yourself, for example, mydaemon, to enable mydaemon start to start the service, mydaemon stop the service, and then copy the script to/etc/rc. d/init. d, and then in/etc/rc. d/rc ?. D To establish a connection. S65mydaemon is connected to rc3.d, and K65mydaemon is in other directories. This way, your script will be automatically processed when you enter and exit runlevel 3. In addition to the run level-related startup commands, Linux also introduced some other configuration files from BSD, the most important of which is/etc/rc. d/rc. local. Generally, it runs after all running scripts are executed. You can customize your settings here, such as welcome information.Differences between versionsAs we can see, the Redhat STARTUP script looks well organized and it is easy to find a function, but it is difficult to find where a command is started, it is often necessary to start from/etc/inittab. The scripts of Turbo Linux and Red-Flag are quite similar to those of Redhat, especially the Chinese version of Red Flag, which has similar configurations. The Slackware STARTUP script uses another style. In fact, it looks like the BSD series. The STARTUP script is also in/etc/rc. d, but it puts together some scripts that must be used at most of the running level, named rc. modules, rc. inet1, rc. inet2 and so on. This is really much simpler for manual configuration of the system, but these files are quite large and seem to require patience. For this system, you only need to read the/etc/inittab once to master its configuration file location. Debian/Corel uses an interesting method that looks quite like Sun's Solaris. In fact, it is almost the same as the RedHat method, but the startup script is not in/etc/rc. d, but directly located under/etc, such as/etc/rc1.d,/etc/rc2.d. After knowing this, debian configuration will not be difficult. For Corel Linux, we should specifically say that its configuration file organization looks simpler than RedHat, but it is not similar to rc. the setting of modules is equivalent to rc. the sysinit file is/etc/init. d/rcS. Its running Level definition is also quite weird. It seems that Run Level 2 is a normal graph mode. to configure Corel, it is best to start directly from/etc/inittab. In any case, we do not recommend a novice to use corel Linux.