# actually the shutdown of the computer is the switch of the running level, the # init process monitors whether the runlevel changes. # If the runlevel changes, the INIT process will trigger the /etc/rc.d/rc script to run. # rc script function is: # 1, if the current computer is running there, at the current run level # (correctrunlevel )/ The service that starts with K in the etc/rc.d/rc$runlevel.d/directory. is closed. # 2, turn on the current runlevel (correct runlevel )/etc/rc.d/rc$runlevel.d/directory # Services that start with S # This enables the control of services that are allowed to start at different operating levels, and that prohibit startup. The # Service script that starts with s in the correct runlevel /etc/rc.d/rc$runlevel.d/directory with a:half will shut down the computer #如果当前运行级别. #[[email protected] rc.d]# ll rc0.d/s*lrwxrwxrwx 1 root root 17 Apr 5 00:13 rc0.d/s00killall ->, .... /init.d/killalllrwxrwxrwx 1 root root 14 apr 5 00:13 rc0.d/ S01HALT&NBSP;->&NBSP, .... /init.d/halt# executes the kill script before executing the halt script. # actually the system administrator issued the "shutdown" command, that is, the notification init process, the operational level changed. [[Email protected] ~]# man shutdown.....shutdown does its job by signalling the init process, asking it to change the runlevel.......# Process init configuration file/etc/inittab define level switch action [[Email protected] ~]# vim /etc/inittabl0:0:wait:/etc/rc.d/rc 0 # performs L1:1:WAIT:/ETC/RC.D/RC at boot time or when changing the operating level 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# These rows indicate that the RC program (/ETC/RC.D/RC script) is run once every time the system's RunLevel is changed,# Also, Init should pass a single-character argument that contains a run-level number (0~~6) to the RC program. # will monitor changes in the operating level of the host, such as the change in operational level, will invoke the:/etc/rc.d/rc script # if the current host is running at: [[email protected] bin]# runlevelN 3# function Command init issue shutdown program signal using: [[email protected] bin]# init The 0 # init process discovers that a change in the run level will follow the l0:0:wait:/etc/rc.d/rc # 0 in profile Inittab. [[email protected] ~]# vim /etc/rc.d/rc#! /bin/bash## rc &nBsp; this file is responsible for starting/stopping# services when the runlevel changes.## Original Author:# miquel van smoorenburg, <[email protected]> #set -m############################################################## ##### #该函数是检查当前运行级别的脚本条件:# 1, the script must have permissions to run;# 2, not part of a function is_ignored_file The type specified in # check a file to be a correct runlevel scriptcheck_ runlevel () { # check if the file exists at all. [ -x "$ ] |" | return 1 is_ignored_file "$" && return 1 # calls the function to check the file type. return 0}# now find out what the current and what the previous runlevel are.argv1= "$" # Record: When the runlevel changes, the INIT process triggers the arguments passed when the RC script is run. set '/sbin/runlevel ' # records the running level of the current host runlevel=$2previous=$1export runlevel Previous. /etc/init.d/functions# see if we want to be in user confirmation modeif [ "$previous" = "N" ]; then if [ -f /var/run/confirm ]; then echo $ "Entering Interactive startup " else echo $ "Entering non-interactive startup " fifi# Get first argument. Set new runlevel to this argument. [ -n "$ARGV 1" ] && runlevel= "$ARGV 1" #如果进程init监控到运行级别改变时, The parameter, is passed when the RC script is triggered #就把该参数当作当前主机的运行级别 # is there an rc directory for this new runlevel? #判断运行级别在 The directory under the/etc/directory, and if not, exit directly. #这也可判断传递的运行参数是否正确 [ -d /etc/rc$runlevel.d ] | | exit 0#################################################################### #给/etc/rc.d/rc$ Script delivery at the beginning of K in the RUNLEVEL.D directory: Stop To enable the shutdown of this runlevel does not start the service # first, run the kill scripts. for i in /etc/rc$runlevel.d/K* ; do check_runlevel "$i" | | continue #调用函数check_runlevel检查脚本, if the startup script for a service goes down, #否则退出当前循环执行下一次循环 ####################### #如果 $i is a service startup script that judges the script (a script starting with K) #在运行级别切换之前是否已经启动. #因为通过脚本启动服务后 (such as:service httpd start or httpd start), #都会在/var/lock/subsys/ Directory to create a file named after the startup script # It is also by judging the/var/lock/subsys/directory that there is no file corresponding to the service script, #来确定该服务是否启动的. # Check if the subsystem is Already up. subsys=${i#/etc/rc$runlevel.d/k??} [ -f /var/lock/subsys/$subsys &NBSP;-O&NBsp;-f /var/lock/subsys/$subsys .init ] | | continue # bring the subsystem down. #关闭当前运行级别设置不启动的服务 (Service, started before run-level switchover #但是该服务在当前运行级别是设置不启动的), if lc_all=c egrep -q "^.. *init.d/functions " $i ; then $i stop else action $ "stopping $subsys: " $ i stop fidone############### start the current run level setting start the service ################################################################### ### now run the start scripts.for i in /etc/rc$runlevel.d/s* ; do check_runlevel "$i" | | continue # check if the subsystem is already up. subsys=${i#/etc/rc$runlevel.d/s??} [ -f /var/lock/subsys/$subsys -o -f / var/lock/subsys/$subsys .init ] && continue #如果运行级别切换之前, the service has been started and exited directly. #防止一个服务启动两次. Directory/var/lock/subsys/The role of the lock file. # if we ' Re in confirmation mode, get user confirmation if [ -f / var/run/confirm ]; then confirm $subsys test $? = 1 && continue fi #如果当前运行级别下 (/etc/rc.d/rc$ RUNLEVEL.D) Half script is the start of S #就会运行该脚本, the script is to turn off the computer, update_boot_stage "$subsys" # Bring the subsystem up. if [ "$subsys" = "Halt" -o "$subsys" = " Reboot " ]; then export LC_ALL=C exec $i start fi if LC_ALL=C egrep -q " ^.. *init.d/functions " $i | | [ "$subsys" = "single" -o "$subsys" = "local" ]; then $i start else action $ "Starting $subsys: " $i start fidonerm -f /var/run/confirmif [ -x /usr/bin/rhgb-client ] && /usr/bin/rhgb-client --ping ; then /usr/bin/rhgb-client --quitfi
This article is from the "Linux" blog, so be sure to keep this source http://9528du.blog.51cto.com/8979089/1420173