简体   繁体   中英

Understand Bash init script start/stop/restart/

We can we a lot of service(bash script) under folder /etc/rc.d/init.d/. 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"

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

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

At some point in time, when going to level 2, the following script loop runs

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. Actually The S is followed by two digits, specifying the execution order.

Same idea for the K* files, when leaving the runlevel.

Now back to your question : this is the job of some lines in the /etc/inittab file

# 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. These scripts are symlinked to the rc directories, like /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. 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.

eg:
S19postgresql
S20clamav-freshclam
S50saned
S70pppd-dns
S99ondemand

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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