再谈Debian的启动脚本

1:先是/etc/inittab。
 id:5:initdefault:
接着:
 si::sysinit:/etc/init.d/rcS //这是和其他dist不同的地方,到底/etc/init.d/rcS干什么呢?
我们先进去 看看再说
================================================

#下面的步骤进入/etc/init.d/rcS看看,等/etc/init.d/rcS执行
#完了,还要返回到/etc/inittab
#
#首先,定义PATH、runlevel、prevlevel然后导出到环境中
#
PATH=/sbin:/bin:/usr/sbin:/usr/bin
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel
#然后,判断是不是第一次安装系统,如果是,则检查并且执行安装
#程序留下的脚本
#/sbin/unconfigured.sh
if [ -x /sbin/unconfigured.sh ]
then
  /sbin/unconfigured.sh
fi

. /etc/default/rcS
export VERBOSE
#捕捉INT、QUIT、TSTP信号,
trap ":" INT QUIT TSTP
#检查/etc/rcS.d/目录,看是否有以S开头并且紧跟两个字符(实际
#上一般是两个数字0-99)命名的
#非普通(! -f "$i")文件,如果有则根据文件的类型作出两个选择
# 1,是.sh结尾的脚本时执行
# 2,如果不是.sh结尾的脚本,则传递给start参数执行这个文件
for i in /etc/rcS.d/S??*
do
 # Ignore dangling symlinks for now.
 [ ! -f "$i" ] && continue
 case "$i" in
  *.sh)
   # Source shell script for speed.
   (
    trap - INT QUIT TSTP
    set start
    . $i
   )
   ;;
  *)
   # No sh extension, so fork subprocess.
   $i start
   ;;
 esac
done

#这是为了兼容其他系统的/etc/rc.boot脚本
[ -d /etc/rc.boot ] && run-parts /etc/rc.boot
#这也是用于第一次安装系统后需要执行的脚本,安装成功后,系统上
#一般没有这个脚本
if [ -x /sbin/setup.sh ]
then
  /sbin/setup.sh
fi
#/etc/rc.S/rcS脚本执行结束.返回/inittab
==========================================
返回/inittab,继续:
这是单用户模式下系统的动作
~~:S:wait:/sbin/sulogin
接下来,根据系统进入的运行级别,执行对应的初始化工作,比如一些启动或者关闭一些服务进程啊什么的:
l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
其实都差不多,进入哪个级别,就以哪个级别作为参数调用/etc/init.d/rc
下面我们看看这个/etc/init.d/rc又有和玄机呢?下面我们又进入/etc/init.d/rc这个世外桃源来看看
=========================================
#定义一个startup()函数
startup() {
  case "$1" in
 *.sh)
  $debug sh "$@"
  ;;
 *)
  $debug "$@"
  ;;
  esac
}
#捕捉INT、QUIT、TSTP信号
  trap ":" INT QUIT TSTP
  # Set onlcr to avoid staircase effect.
#设置终端,将 CR 字符映射为 NL 字符,避免阶梯效应
  stty onlcr 0>&1
  # Now find out what the current and what the previous runlevel are.
  runlevel=$RUNLEVEL
  # Get first argument. Set new runlevel to this argument.
  [ "$1" != "" ] && runlevel=$1
  if [ "$runlevel" = "" ]
  then
 echo "Usage: $0 <runlevel>" >&2
 exit 1
  fi
  previous=$PREVLEVEL
  [ "$previous" = "" ] && previous=N
  export runlevel previous
  # Is there an rc directory for this new runlevel?
  if [ -d /etc/rc$runlevel.d ]
  then
 # First, run the KILL scripts.
 if [ $previous != N ]
 then
  for i in /etc/rc$runlevel.d/K[0-9][0-9]*
  do
   # Check if the script is there.
   [ ! -f $i ] && continue
   # Stop the service.
   startup $i stop
  done
 fi
 # Now run the START scripts for this runlevel.
 for i in /etc/rc$runlevel.d/S*
 do
  [ ! -f $i ] && continue
  if [ $previous != N ] && [ $previous != S ]
  then
   #
   # Find start script in previous runlevel and
   # stop script in this runlevel.
   #
   suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
   stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
   previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
   #
   # If there is a start script in the previous level
   # and _no_ stop script in this level, we don't
   # have to re-start the service.
   #
   [ -f $previous_start ] && [ ! -f $stop ] && continue
  fi
  case "$runlevel" in
   0|6)
    startup $i stop
    ;;
   *)
    startup $i start
    ;;
  esac
 done
  fi
# eof /etc/init.d/rc
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值