Linux 目录之 /etc/init.d/ 介绍

https://blog.csdn.net/qq_37212828/article/details/107227965

本目录下的所有文件

[root@EPC-M6G2C init.d]# ls
S01logging*          S30dbus*             S50telnet*           S90start_userapp.sh* 
S10udev*             S40network*          S70vsftpd*           socketcand*
S13portmap*          S50dropbear*         S80mount-opt*        rcK*
S20urandom*          S50sshd*             S81web.sh*           rcS*

先看一下rcS文件,它是本目录在开机时最先启动的文件

rcS文件

#!/bin/sh

# Start all init scripts in /etc/init.d
# executing them in numerical order.
#
for i in /etc/init.d/S??* ;do
     # Ignore dangling symlinks (if any).
     [ ! -f "$i" ] && continue #如果是目录或设备文件则遍历下一个
     case "$i" in
        *.sh) #.sh结尾的脚本
            # Source shell script for speed.
            (
                trap - INT QUIT TSTP  #重置陷阱为启动shell时的陷阱
                set start #设置传入的参数
                . $i #执行,携带start参数
            )
            ;;
        *) #其他脚本
            # No sh extension, so fork subprocess.
            $i start #执行,携带start参数
            ;;
    esac
done

总结:检测大写S开头的所有脚本并执行它们。

rcK文件是设备关机或重启执行的文件,它和rcS差不多,只有传入的参数有微微差别

rcK文件

#!/bin/sh

# Stop all init scripts in /etc/init.d
# executing them in reversed numerical order.
#
for i in $(ls -r /etc/init.d/S??*) ;do #$()相当于反引号,首先执行命令,-r倒序排列
     # Ignore dangling symlinks (if any).
     [ ! -f "$i" ] && continue #如果不存在则遍历下一个
     case "$i" in
        *.sh)
            # Source shell script for speed.
            (
                trap - INT QUIT TSTP
                set stop
                . $i #执行,携带stop参数
            )
            ;;
        *)
            # No sh extension, so fork subprocess.
            $i stop #执行,携带stop参数
            ;;
    esac
done

总结:关机时还需要启动一遍启动脚本,只不过传入的参数是stop,需要处理一些后续工作才关机。

file:S01logging

#!/bin/sh
#
# Start logging
#

start() {
        echo -n "Starting logging: "
        start-stop-daemon -b -S -q -m -p /var/run/syslogd.pid --exec /sbin/syslogd -- -n
        start-stop-daemon -b -S -q -m -p /var/run/klogd.pid --exec /sbin/klogd -- -n
        echo "OK"
}

stop() {
        echo -n "Stopping logging: "
        start-stop-daemon -K -q -p /var/run/syslogd.pid
        start-stop-daemon -K -q -p /var/run/klogd.pid
        echo "OK"
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        stop
        start
        ;;
  *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $? #最后一次命令的执行状态码,0为正常。

由rcS或者rcK脚本调用的子进程运行此脚本,传递start或stop参数来控制执行start函数或stop函数。
start-stop-daemon守护进程启停工具命令
参数
-b | --background 后台运行
-S | --start – 开启一个系统守护程序,并传递参数给它
-K | --stop 停止一个程序
-q | --quiet 不要输出警告
-m| --make-pidfile 当命令本身不创建pidfile时,由start-stop-daemon创建
-p | --pidfile 要检查的pid文件
-x | --exec 程序启动/检查它是否正在运行
-n | --name 要检查的进程名称

file:S10udev

#!/bin/sh
#
# udev  This is a minimal non-LSB version of a UDEV startup script.  It
#       was derived by stripping down the udev-058 LSB version for use
#       with buildroot on embedded hardware using Linux 2.6.34+ kernels.
#
#       You may need to customize this for your system's resource limits
#       (including startup time!) and administration.  For example, if
#       your early userspace has a custom initramfs or initrd you might
#       need /dev much earlier; or without hotpluggable busses (like USB,
#       PCMCIA, MMC/SD, and so on) your /dev might be static after boot.
#
#       This script assumes your system boots right into the eventual root
#       filesystem, and that init runs this udev script before any programs
#       needing more device nodes than the bare-bones set -- /dev/console,
#       /dev/zero, /dev/null -- that's needed to boot and run this script.
#

# Check for missing binaries
UDEV_BIN=/sbin/udevd
test -x $UDEV_BIN || exit 5 #文件不可执行则退出,退出码:5

# Check for config file and read it
UDEV_CONFIG=/etc/udev/udev.conf
test -r $UDEV_CONFIG || exit 6 #文件不可读则退出,退出码:6
. $UDEV_CONFIG

case "$1" in
    start) #设备开机时
        printf "Populating ${udev_root:-/dev} using udev: "
        printf '\000\000\000\000' > /proc/sys/kernel/hotplug
        $UDEV_BIN -d || (echo "FAIL" && exit 1) #执行失败打印“FAIL”并退出
        udevadm trigger --type=subsystems --action=add
        udevadm trigger --type=devices --action=add
        udevadm settle --timeout=30 || echo "udevadm settle failed"
        echo "done"
        ;;
    stop) #设备关机或重启
        # Stop execution of events
        udevadm control --stop-exec-queue
        killall udevd
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac

exit 0

udevadm trigger [options]  接收内核发送来的设备事件。主要用于重放coldplug事件信息
–type=type         触发一个特殊的设备。合法的类型:devices,subsystem,failed.默认是devices
–action=action      被触发的事件,默认是change

udevadm settle [options]  查看udev事件队列,如果所有事件全部处理完就退出。
–timeout=seconds     等待事件队列空的最大时间。默认是180秒。如果是0则立即退出。

udevadm control [options]   控制udev守护进程(systemd-udevd)的内部状态。
-s, --stop-exec-queue    向 systemd-udevd 发送"禁止处理事件"信号, 这样所有新发生的事件都将进入等候队列。

file:S13portmap

#! /bin/sh

[ -f /sbin/portmap ] || exit 0 #不存在此文件就退出

start() {
        echo -n "Starting portmap: "
        portmap
        mkdir -p /var/lock/subsys
        touch /var/lock/subsys/portmap #锁定此服务
        echo "done"
}

stop() {
        echo -n "Stopping portmap: "
        echo
        killall portmap
        rm -rf /var/lock/subsys
        echo "done"
}

restart() {
        stop
        start
        rm -f /var/run/portmap.state
}

# See how we were called.
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart|reload)
        restart
        ;;
  *)
        echo "Usage: $0 {start|stop|reload|restart}"
        exit 1
esac

exit $? 

file:S20urandom

#! /bin/sh
#
# urandom       This script saves the random seed between reboots.
#               It is called from the boot, halt and reboot scripts.
#
# Version:      @(#)urandom  1.33  22-Jun-1998  miquels@cistron.nl
#

[ -c /dev/urandom ] || exit 0  #不存在字符设备urandom就退出
#. /etc/default/rcS

case "$1" in
        start|"")
                # check for read only file system
                if ! touch /etc/random-seed 2>/dev/null #如果无法创建random-seed文件
                then
                        echo "read-only file system detected...done" #检测到只读文件系统
                        exit #退出
                fi
                if [ "$VERBOSE" != no ]
                then
                        echo -n "Initializing random number generator... "
                fi
                # Load and then save 512 bytes,
                # which is the size of the entropy pool
                cat /etc/random-seed >/dev/urandom
                rm -f /etc/random-seed
                umask 077  #配置文件夹默认权限
                dd if=/dev/urandom of=/etc/random-seed count=1 \
                        >/dev/null 2>&1 || echo "urandom start: failed."
                umask 022
                [ "$VERBOSE" != no ] && echo "done."
                ;;
        stop)
                if ! touch /etc/random-seed 2>/dev/null
                then
                        exit
                fi
                # Carry a random seed from shut-down to start-up;
                # see documentation in linux/drivers/char/random.c
                [ "$VERBOSE" != no ] && echo -n "Saving random seed... "
                umask 077
                dd if=/dev/urandom of=/etc/random-seed count=1 \
                        >/dev/null 2>&1 || echo "urandom stop: failed."
                [ "$VERBOSE" != no ] && echo "done."
                ;;
        *)
                echo "Usage: urandom {start|stop}" >&2
                exit 1
                ;;
esac
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值