『學了就忘』Linux服務管理 — 78、獨立服務的啟動指令碼分析
阿新 • • 發佈:2021-12-13
既然獨立的服務啟動是依靠/etc/init.d/服務名
這個指令碼來進行啟動管理的,那麼這個指令碼中到底是什麼樣子的?我們以apache
服務的啟動指令碼來學習一下啟動指令碼到底是怎麼實現一個服務啟動的。
提示:/etc/init.d/服務名
和/etc/rc.d/init.d/服務名
是一樣的,軟連結關係。
檢視apache
的httpd
服務,如下所示:
[root@localhost ~]# vim /etc/rc.d/init.d/httpd 內容如下: #!/bin/bash # # httpd Startup script for the Apache HTTP Server # # 一、以下兩句話是能被chkcongif命令識別的,用於apache自啟動。其他的就都是註釋資訊了。 # 1.自啟動設定:代表自啟動級別,85(S85)代表啟動序號,15(K15)代表關閉序號。 # 這個可以自定義,只要別和其他服務重疊即可。 # chkconfig: -85 15 # # 2.服務描述。 # description:The Apache HTTP Server is an efficient and extensible\ # server implementing the current HTTP standards. # # processname:httpd # config:/etc/httpd/conf/httpd.conf # config:/etc/sysconfig/httpd # pidfile:/var/run/httpd/httpd.pid # ### BEGIN INIT INFO # Provides:httpd # Required-Start:$local_fs $remote_fs $network $named # Required-Stop:$local_fs $remote_fs $network # Should-Start:distcache # Short-Description:start and stop Apache HTTP Server # Description:The Apache HTTP Server is an extensible server # implementing the current HTTP standards. ### END INIT INFO # 以上都是註釋。 # 二、下面兩個進行apache的函式呼叫 # Source function library. # ”.”其實就是source,就是呼叫functions檔案。 ./etc/rc.d/init.d/functions # 判斷httpd如果是檔案,則呼叫httpd檔案。 if[-f/etc/sysconfig/httpd];then ./etc/sysconfig/httpd fi # 三、以下開始定義變數 # 定義變數HTTPD_LANG的值。並追加變數的值為C,即英文。 # Start httpd in the C locale by default. HTTPD_LANG=${HTTPD_LANG-"C"} # This will prevent initlog from swallowing up a pass-phrase prompt if # mod_ssl needs a pass-phrase from the user. INITLOG_ARGS="" # Set HTTPD=/usr/sbin/httpd.worker in/etc/sysconfig/httpd to use a server # with the thread-based ”worker" MPM;BE WARNED that some modules may not # work correctly with a thread-based MPM;notably PHP will refuse to start. # Path to the apachectl script,server binary,and short-form for messages. #定義一系列變數,用於後面的執行。 apachect1=/usr/sbin/apachectl httpd=${HTTPD-/usr/sbin/httpd} prog=httpd pidfile=${PIDFILE-/var/run/httpd/httpd.pid} lockfile=${LOCKFILE-/var/lock/subsys/httpd} # 定義全域性命令返回變數。 RETVAL=0 STOP TIMEOUT=${STOP TIMEOUT-10} # 四、下面是定義的函式 # The semantics of these two functions differ from the way apachectl does # things -- attempting to start while running is a failure, and shut down # when not running is also a failure. So we just do it the way init scripts # are expected to behave here. # 定義start函式,用於apache的啟動。 # 如果守護程序/usr/sbin/httpd啟動成功($RETVAL =0), # 就建立/var/lock/subsys/httpd檔案(touch ${lockfile} ) # 通過$httpd變數執行/usr/sbin/httpd命令啟動apache。 # 通過$pidfile變數呼叫apache的PID。 # 通過變數$OPTIONS定義命令執行時的初始化環境配置,依賴/etc/sysconfig/httpd檔案。 start() { echo -n $"Starting $prog: " LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS RETVAL=$? echo [ $RETVAL = 0 ] && touch ${lockfile} return $RETVAL } # When stopping httpd, a delay (of default 10 second) is required # before SIGKILLing the httpd parent; this gives enough time for the # httpd parent to SIGKILL any errant children. # 定義stop函式,用來關閉apache服務,關閉服務之後會刪除pid檔案。 stop() { status -p ${pidfile} $httpd > /dev/null if [[ $?= 0 ]]; then echo -n $"Stopping $prog:" killproc -p ${ pidfile} -d ${STOP_TIMEOUT} $httpd else echo -n $"Stopping $prog:" success fi RETVAL=$? echo [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} } # 定義reload函式,用於apache的重新載入。 # 通過/usr/sbin/httpd -t命令判斷apache的配置檔案。 # 如果配置檔案報錯,則輸出錯誤提示。 # 如果配置檔案正確,則重新載入apache。 reload() { echo -n $"Reloading $prog:" if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then RETVAL=6 echo $"not reloading due to configuration syntax error" failure $"not reloading $httpd due to configuration syntax error" else # Force LSB behaviour from killproc LSB=1 ki11proc -p ${pidfile} $httpd -HUP RETVAL=$? if [ $RETVAL -eq 7 ]; then failure $"httpd shutdown" fi fi echo } # 五、下面是真正的程式體 # See how we were called. # 判斷執行指令碼後的第一個引數的值,$1表示執行指令碼時的第一個引數。 case "$1" in # 如果引數值為start,則呼叫start函式。 start) start ;; ;; # 如果引數值為stop,則呼叫stop函式。 stop) stop ;; # 如果引數值為status,則執行status-p Shttpd命令測試apache狀態。 status) status -p ${pidfile} $httpd RETVAL=$? ;; # 如果引數值為restart,則先呼叫stop函式,再呼叫start函式 restart) stop start ;; # 如果引數值為condrestart或try-restart, # 則只有apache服務是已經執行時才先呼叫stop函式,再呼叫start函式,重啟apache。 # 如果apache服務沒有執行,則不重啟apache. condrestartltry-restart) if status -p ${pidfile} $httpd >&/dev/null;then stop start fi ;; # 如果引數值為force-reload或reload,則呼叫reload函式。 force-reload|reload) reload ;; # 如果引數是graceful或help或configtest或fullstatus, # 則執行/usr/sbin/apachectl命令,並把引數作為命令的引數傳入apachectl命令。 graceful|help|configtest|ful1status) $apachectl $@ RETVAL=$? ;; # 如果輸出的引數不是以上任何引數,則輸出錯誤資訊 *) echo $"Usage:$pro{start lstoplrestartl condrestartl try-restartl force-reloadlreload status full status graceful l he lpl confis test}" RETVAL=2 esac exit $RETVAL
說明:
我們分析服務的啟動指令碼,主要是為了學習一下啟動指令碼寫法。
當我們執行一個服務啟動命令,例如:[root@localhost ~]# /etc/rc.d/init.d/httpd start
start
作為一個引數,傳入指令碼中,執行指令碼程式體中start
引數所對應的程式碼。
然後呼叫start
函式,來執行啟動命令。
而start
函式中又用了很多在啟動指令碼前面定義的很多變數。
最終把一個服務啟動起來。