#! /bin/bash
#
# rc This file is responsible for starting/stopping
# services when the runlevel changes.
#
# Original Author:
# Miquel van Smoorenburg, <[email protected]>
#
# check a file to be a correct runlevel script
check_runlevel ()
{
# Check ifthe file exists at all. -x:检测该文件名是否具有可执行属性
[ -x"$1" ] || return 1
# Rejectbackup files and files generated by rpm.
case "$1"in
*.rpmsave|*.rpmorig|*.rpmnew|*~|*.orig)
return1
;;
esac
return 0
}
# Now find out what the current and what the previousrunlevel are.
argv1="$1"
set `/sbin/runlevel` #set a b : $1=a, $2=b,$#=2
#runlevel 结果:N 5,表示前一个运行级N(没有),当前运行级为5
#另外`/sbin/runlevel`可用$(/sbin/runlevel)替换
runlevel=$2
previous=$1
export runlevel previous
. /etc/init.d/functions
# functions文件中定义了一些函数如:checkpid(),daemon(),killproc(),
# pidfileofproc(),pidofproc(),status(),echo_success(),echo_failure()
# echo_passed(),echo_waring(),success(),failure(),passed(),warning()
# action(),strstr(),confirm(),
# . /etc/init.d/functions表示在当前shell中执行functions命令,此时会把functions
# 文件中函数的定义读到当前的shell中
# See if we want to be in user confirmation mode
# -f 该“文件名”是否为文件, || 如果前面的执行失败则继续执行后面的,否则不执行
# grep -i 表示忽略大小写的不同
if [ "$previous" ="N" ]; then
if [ -f /var/run/confirm ] \
|| grep -i confirm /proc/cmdline>/dev/null ; then
rm -f/var/run/confirm
CONFIRM=yes
exportCONFIRM
echo$"Entering interactive startup"
else
echo$"Entering non-interactive startup"
fi
fi
# Get first argument. Set new runlevel to thisargument.
# -n 判断字符串是否非为0,若为空则为false
[ -n "$argv1" ] &&runlevel="$argv1"
# Is there an rc directory for this new runlevel?
# -d 判断文件名是否为目录文件
[ -d /etc/rc$runlevel.d ] || exit 0
# First, run the KILL scripts.
# /var/lock 某些设备具有一次性写入特性,此时为了避免被其它人干扰正在运地的操作,因此,会将该设备lock(锁)起来,以确定该设备只能被单一程序所用
#${var#pattern}:表示从var的左边去掉最小匹配串
#${i#/etc/rc$runlevel.d/K??}:表示从i的最左边去掉“K??”三个字符,“?”表匹配任意一个字符
#-q 禁止所有的输出到标准输出,不管匹配行。如果选中输入行,以0 状态退出。
for i in /etc/rc$runlevel.d/K* ; do
check_runlevel"$i" || continue
# Check ifthe subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/K??}
[ -f/var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
||continue
# Bring thesubsystem down.
if egrep -q"(killproc |action )" $i ; then
$i stop
else
action$"Stopping $subsys: " $i stop
fi
done
# Now run the START scripts.
for i in /etc/rc$runlevel.d/S* ; do
check_runlevel"$i" || continue
# Check ifthe subsystem is already up.
subsys=${i#/etc/rc$runlevel.d/S??}
[ -f/var/lock/subsys/$subsys -o -f /var/lock/subsys/$subsys.init ] \
&&continue
# If we're inconfirmation mode, get user confirmation
# $? :表示上个命令的返回结果,如果执行成功,则返回0
if [ -n "$CONFIRM" ]; then
confirm $subsys
case $? in
0) :;;
2)CONFIRM=;;
*)continue;;
esac
fi
# Bring thesubsystem up.
if ["$subsys" = "halt" -o "$subsys" ="reboot" ]; then
exportLC_ALL=C
exec $istart
fi
if egrep -q"(daemon |action |success |failure )" $i 2>/dev/null \
|| ["$subsys" = "single" -o "$subsys" ="local" ]; then
$i start
else
action$"Starting $subsys: " $i start
fi
done