简体   繁体   English

了解 Bash init 脚本启动/停止/重启/

[英]Understand Bash init script start/stop/restart/

We can we a lot of service(bash script) under folder /etc/rc.d/init.d/.我们可以在 /etc/rc.d/init.d/ 文件夹下提供很多服务(bash 脚本)。 And they all look like this:它们看起来都像这样:

case "$1" in 
start)   echo "start" ;;
stop)    echo "stop" ;;
restart) echo "restart" ;;
esac

I just don't understand we I boot my computer, how kernel call those startup script and pass in parameter "start" Or when the service dies, who call script and pass in parameter "restart"我只是不明白我们启动我的计算机,内核如何调用这些启动脚本并传入参数“start” 或者当服务终止时,谁调用脚本并传入参数“restart”

Can someone explain this to me?谁可以给我解释一下这个?

Thanks in advance.提前致谢。

It depends on your distribution / version / configuration choices.这取决于您的发行版/版本/配置选择。 For Debian with System V-style startup files, please have a look to http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit对于带有 System V 风格启动文件的 Debian,请查看http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit

Roughly, each runlevel has a directory, for example /etc/rc2.d for level 2, containing symbolic links to regular files (scripts) in /etc/init.d粗略地说,每个运行级别都有一个目录,例如 /etc/rc2.d 用于级别 2,包含指向 /etc/init.d 中常规文件(脚本)的符号链接

At some point in time, when going to level 2, the following script loop runs在某个时间点,当进入第 2 级时,会运行以下脚本循环

for s in /etc/rc2.d/S* 
do
   $s start
done

starting the execution of all links with names starting with an S, in alphabetical order.按字母顺序开始执行名称以 S 开头的所有链接。 Actually The S is followed by two digits, specifying the execution order.实际上 S 后面是两位数字,指定执行顺序。

Same idea for the K* files, when leaving the runlevel.离开运行级别时,K* 文件的想法相同。

Now back to your question : this is the job of some lines in the /etc/inittab file现在回到你的问题:这是 /etc/inittab 文件中一些行的工作

# The default runlevel.
id:2:initdefault:

....
# /etc/init.d executes the S and K scripts upon change
# of runlevel.
#
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.

l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
...

The actual location for init shell scripts is under /etc/init.d. init shell 脚本的实际位置在 /etc/init.d 下。 These scripts are symlinked to the rc directories, like /etc/rc0.d, /etc/rc1.d, /etc/rc2.d.这些脚本符号链接到 rc 目录,如 /etc/rc0.d、/etc/rc1.d、/etc/rc2.d。 Then, within each rcn.d directory, we have files that start with either K or S in their file name, followed by two digits.然后,在每个 rcn.d 目录中,我们的文件名以 K 或 S 开头,后跟两位数字。 These are symbolic link files that point back to the actual init shell scripts, where K means Kill (ie stop) and "S" stands for Start.这些是指向实际 init shell 脚本的符号链接文件,其中 K 表示终止(即停止),而“S”表示启动。

eg:例如:
S19postgresql S19postgresql
S20clamav-freshclam S20clamav-freshclam
S50saned S50saned
S70pppd-dns S70pppd-dns
S99ondemand S99点播

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM