Chinaunix首页 | 论坛 | 博客
  • 博客访问: 743699
  • 博文数量: 285
  • 博客积分: 11
  • 博客等级: 民兵
  • 技术积分: 2340
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-23 17:58
个人简介

真正的安全感,来自你对自己的信心,是你每个阶段性目标的实现,而真正的归属感,在于你的内心深处,对自己命运的把控,因为你最大的对手永远都是自己。| Network+Security+Database.

文章存档

2011年(3)

2010年(1)

2009年(33)

2008年(248)

我的朋友

分类: LINUX

2008-09-20 13:09:08

  从/etc/inittab中可以看出,启动的核心就是rc脚本(即为主要运行级别控制脚本 /etc/rc.d/init.d/rc).
  首先init程序调用rc脚本,并将级别传递给rc脚本.
  其次rc脚本去调用该级别内(/etc/rcX.d)各项服务的脚本.

  rc脚本的注释如下:
#!/bin/sh
########################################################################
# Begin $rc_base/init.d/rc
#
# Description : Main Run Level Control Script
#
# Authors     : Gerard Beekmans  - gerard@linuxfromscratch.org
#
# Version     : 00.00
#
# Notes       :
#
########################################################################

. /etc/sysconfig/rc        #指定rc_bash=/etc/rc.d rc_functions = ${rc_bash}/init.d/functions network_devices=/etc/sysconfig/network-devices
. ${rc_functions}

# This sets a few default terminal options.
stty sane

# These 3 signals will not cause our script to exit
trap 
"" INT QUIT TSTP    #trap是bash内建的命令,用来指定信号的行为,“”为忽略该信号

"${1}" != "" ] && runlevel=${1}        #从参数中得到运行级别,在/etc/inittab中指定的

if [ "${runlevel}" = "" ]; then            #运行级别为空则退出
    
echo "Usage: ${0} " >&2
    
exit 1
fi

previous
=${PREVLEVEL}                    #前一个级别
"${previous}" = "" ] && previous=N    #没有前一级别则为n

if [ ! -d ${rc_base}/rc${runlevel}.d ]; then    #是否有该级别的目录
    boot_mesg 
"${rc_base}/rc${runlevel}.d does not exist." ${WARNING}
    boot_mesg_flush
    
exit 1
fi

# Attempt to stop all service started by previous runlevel,
# and killed in this runlevel
#结束该级别中K开头的脚本
if [ "${previous}" != "N" ]; then        #如果有前一个级别
    
for i in $(ls -v ${rc_base}/rc${runlevel}.d/K* 2> /dev/null)
    
do
        check_script_status

        suffix
=${i#$rc_base/rc$runlevel.d/K[0-9][0-9]}        #脚本的名称,除去路径和K**
        prev_start
=$rc_base/rc$previous.d/S[0-9][0-9]$suffix    #前一级别
        sysinit_start
=$rc_base/rcsysinit.d/S[0-9][0-9]$suffix    #系统初始化

        
if [ "${runlevel}" != "0" ] && [ "${runlevel}" != "6" ]; then        #不是重启和关机
            
if [ ! -f ${prev_start} ] && [ ! -f ${sysinit_start} ]; then    #如果不存在
                boot_mesg -n 
"WARNING: ${i} can't be" ${WARNING}            #前一级别并没有启动该脚本
                boot_mesg -n 
" executed because it was not"
                boot_mesg -n 
" not started in the previous"
                boot_mesg -n 
" runlevel (${previous})."
                boot_mesg 
"" ${NORMAL}
                boot_mesg_flush
                continue
            fi
        fi
        
${i} stop        #脚本执行stop
        error_value
=${?}    #返回值

        
if [ "${error_value}" != "0" ]; then
            print_error_msg
        fi
    done
fi

#Start all functions in this runlevel
#开始所有S开头的脚本
for i in $( ls -v ${rc_base}/rc${runlevel}.d/S* 2> /dev/null)        #获得每个脚本
do
    
if [ "${previous}" != "N" ]; then                #如果有前一个级别
        suffix
=${i#$rc_base/rc$runlevel.d/S[0-9][0-9]}    #脚本文件名
        stop
=$rc_base/rc$runlevel.d/K[0-9][0-9]$suffix    
        prev_start
=$rc_base/rc$previous.d/S[0-9][0-9]$suffix

        [ -f 
${prev_start} ] && [ ! -f ${stop} ] && continue    #如果前一个级别中没有该脚本的stop,则说明该进程已经存在
    fi

    check_script_status    
#检查脚本是否存在

    case 
${runlevel} in    #0或6则直接stop
        
0|6)
            
${i} stop
            
;;
        *
)
            
${i} start
            
;;
    esac
    error_value
=${?}

    
if [ "${error_value}" != "0" ]; then
        print_error_msg
    fi
done

# End $rc_base/init.d/rc
阅读(845) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~