天天看點

linux 系統rc.d啟動腳本剖析

轉載位址:https://blog.csdn.net/cooling88/article/details/51049893

linux有自己一套完整的啟動體系,抓住了linux啟動的脈絡,linux的啟動過程将不再神秘。

本文中假設inittab中設定的init tree為: 

(0-6對應系統的7個運作級别)

/etc/rc.d/rc0.d
/etc/rc.d/rc1.d
/etc/rc.d/rc2.d
/etc/rc.d/rc3.d
/etc/rc.d/rc4.d
/etc/rc.d/rc5.d
/etc/rc.d/rc6.d
/etc/rc.d/init.d
           
目錄
  1. 關于linux的啟動
  2. 關于rc.d
  3. 啟動腳本示例
  4. 關于rc.local
  5. 關于bash啟動腳本
  6. 關于開機程式的自動啟動
    1. 關于linux的啟動
               

init是所有程序的頂層

init讀取/etc/inittab,執行rc.sysinit腳本(注意檔案名是不一定的,有些unix甚至會将語句直接寫在inittab中)

rc.sysinit腳本作了很多工作:

init $PATH

config network

start swap function

set hostname

check root file system, repair if needed

check root space

….

rc.sysinit根據inittab執行rc?.d所指向的腳本

linux是多使用者系統,getty是多使用者與單使用者的分水嶺,在getty之前運作的是系統腳本。

2 關于rc.d
           

所有啟動腳本放置在 /etc/rc.d/init.d下

[[email protected] rc.d]# ll init.d/
總用量 256
-rwxr-xr-x.  root root   月   auditd
-rwxr-xr-x.  root root   月    cgconfig
-rwxr-xr-x.  root root   月    cgred
-rwxr-xr-x.  root root   月    crond
-rwxr-xr-x   root root   月    : dnsmasq
-rwxr-xr-x   root root   月     firstboot
-rw-r--r--.  root root  月    functions
-rwxr-xr-x   root root   月   haldaemon
-rwxr-xr-x.  root root   月    halt
-rwxr-xr-x.  root root  月    ip6tables
-rwxr-xr-x.  root root  月    iptables
-rwxr-xr-x.  root root  月    kdump
-rwxr-xr-x.  root root    月    killall
-rwxr-xr-x.  root root   月    mdmonitor
-rwxr-xr-x   root root   月    messagebus
-rwxr-xr-x.  root root   月    netconsole
           

rc?.d中放置的是init.d中腳本的連結,命名格式是:

[[email protected] rc.d]# ll rc0.d/
總用量 
lrwxrwxrwx   root root  月   : K05wdaemon -> ../init.d/wdaemon
lrwxrwxrwx.  root root  月    : K10saslauthd -> ../init.d/saslauthd
lrwxrwxrwx.  root root  月    : K15svnserve -> ../init.d/svnserve
lrwxrwxrwx   root root  月    : K25sshd -> ../init.d/sshd
lrwxrwxrwx.  root root  月    : K30postfix -> ../init.d/postfix
lrwxrwxrwx   root root  月   : K30spice-vdagentd -> ../init.d/spice-vdagentd
lrwxrwxrwx   root root  月    : K50dnsmasq -> ../init.d/dnsmasq
lrwxrwxrwx   root root  月    : K50kdump -> ../init.d/kdump
lrwxrwxrwx.  root root  月    : S00killall -> ../init.d/killall
lrwxrwxrwx.  root root  月    : S01halt -> ../init.d/halt
           
S{number}{name} / K{number}{name}

  S開始的檔案向腳本傳遞start參數

  K開始的檔案向腳本傳遞stop參數

  number決定執行的順序

3. 啟動腳本示例
           

這是一個用來啟動httpd的 /etc/rc.d/init.d/apache 腳本:

代碼:

#!/bin/bash
#!/bin/sh
#
# chkconfig: 345 68 32
...
...
# one is reported.  Run "apachectl help" for usage info
#
ARGV="$@"
#
# |||||||||||||||||||| START CONFIGURATION SECTION  ||||||||||||||||||||
# --------------------                              --------------------
#
# the path to your httpd binary, including options if necessary
HTTPD='/usr/local/apache/bin/httpd'
#
# pick up any necessary environment variables
if test -f /usr/local/apache/bin/envvars; then
  . /usr/local/apache/bin/envvars
fi
#
# a command that outputs a formatted text version of the HTML at the
# url given on the command line.  Designed for lynx, however other
# programs may work.
LYNX="lynx -dump"
#
# the URL to your server's mod_status status page.  If you do not
# have one, then status and fullstatus will not work.
STATUSURL="http://localhost:80/server-status"
#
# Set this variable to a command that increases the maximum
# number of file descriptors allowed per child process. This is
# critical for configurations that use many file descriptors,
# such as mass vhosting, or a multithreaded server.
ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`"
# --------------------                              --------------------
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
fi

ERROR=
if [ "x$ARGV" = "x" ] ; then
    ARGV="-h"
fi

case $ARGV in
start|stop|restart|graceful|graceful-stop)
    $HTTPD -k $ARGV
    ERROR=$?
    ;;
startssl|sslstart|start-SSL)
    echo The startssl option is no longer supported.
    echo Please edit httpd.conf to include the SSL configuration settings
    echo and then use "apachectl start".
    ERROR=
    ;;
configtest)
    $HTTPD -t
    ERROR=$?
    ;;
status)
    $LYNX $STATUSURL | awk ' /process$/ { print; exit } { print } '
    ;;
fullstatus)
    $LYNX $STATUSURL
    ;;
*)
    $HTTPD $ARGV
    ERROR=$?
esac

exit $ERROR
           

可以看出他接受start,stop,restart,status參數

然後可以這樣建立rc?.d的連結:

代碼:

cd /etc/rc.d/init.d && ll

ln -sf ../init.d/apache ../rc0.d/K28apache &&

ln -sf ../init.d/apache ../rc1.d/K28apache &&

ln -sf ../init.d/apache ../rc2.d/K28apache &&

ln -sf ../init.d/apache ../rc3.d/S32apache &&

ln -sf ../init.d/apache ../rc4.d/S32apache &&

ln -sf ../init.d/apache ../rc5.d/S32apache &&

ln -sf ../init.d/apache ../rc6.d/K28apache
           
4. 關于bash啟動腳本
           

/etc/profile 

/etc/bashrc

~/.bash_profile 

~/.bashrc

他們的具體作用介紹如下:

/bin/bash這個指令解釋程式(後面簡稱shell)使用了一系列啟動檔案來建立一個運作環境:

/etc/profile 和 ~/.bash_profile 是在啟動一個互動登陸shell的時候被調用。

/etc/bashrc 和 ~/.bashrc 是在一個互動的非登陸shell啟動的時候被調用。

~/.bash_logout 在使用者登出登陸的時候被讀取并執行。

一個互動的登陸shell會在 /bin/login 成功登陸之後運作。

一個互動的非登陸shell是通過指令行來運作的,如[prompt]$/bin/bash。

一般一個非互動的shell出現在運作 shell腳本的時候。之是以叫非互動的shell,是因為它不在指令行上等待輸入而隻是執行腳本程式。

6. 關于開機程式的自動啟動
           

系統腳本可以放置在/etc/rc.d/init.d中并建立/etc/rc.d/rc?.d連結,也可以直接放置在/etc/rc.d/rc.local中。

init.d腳本包含完整的start,stop,status,reload等參數,是标準做法,推薦使用。

fs