1. 程式人生 > 其它 >|NO.Z.00007|——————————|ManageMent|——|Linux&服務管理.V03|

|NO.Z.00007|——————————|ManageMent|——|Linux&服務管理.V03|



[LinuxSystemEnd:Linux&服務管理.V03]                                                          [Applications.LinuxSystemEnd] [|Linux|系統管理|服務管理|獨立的服務|基於xinetd服務|httpd啟動指令碼分析|原始碼包服務管理|服務優化建議|]








一、RPM 包預設安裝的服務管理
### --- 獨立服務管理
### --- 獨立服務的啟動管理

~~~     使用/etc/init.d/目錄中的啟動指令碼啟動服務
[root@localhost ~]# /etc/init.d/httpd start
~~~     使用 service 命令來啟動獨立的服務
[root@localhost ~]# service 獨立服務名 start|stop|restart|…
二、獨立服務的自啟動管理
### --- 使用 chkconfig 服務自啟動管理命令
~~~     選項:
~~~     --level:    設定在哪個執行級別中開機自啟動(on),或是關閉自啟動(off)

[root@localhost ~]# chkconfig [--level 執行級別] [獨立服務名] [on|off]
[root@server21 ~]# chkconfig --list | grep httpd
httpd           0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@server21 ~]# chkconfig httpd on                               // 開啟開機自啟動
[root@server21 ~]# chkconfig --list | grep httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@server21 ~]# chkconfig httpd off                              // 關閉開機自啟動
### --- 修改/etc/rc.d/rc.local 檔案,設定服務自啟動
~~~     若是讓兩個開機自啟動都生效,會報錯。開機會自啟動2次,建議配置一個。

[root@localhost ~]# vim /etc/rc.d/rc.local                          // 推薦使用此方法設定開機自啟動
touch /var/lock/subsys/local
/etc/rc.d/init.d/httpd  start
### --- 使用 ntsysv 命令管理自啟動
~~~     選項:
~~~     --level 執行級別:可以指定設定自啟動的執行級別,這個命令的操作是這樣的:
~~~     上下鍵:在不同服務之間移動
~~~     空格鍵:選定或取消服務的自啟動。就是在服務之前是否打入“*”
~~~     tab 鍵:在不同專案間切換
~~~     F1 鍵:顯示服務的說明
 
[root@localhost ~]# ntsysv [--level 執行級別]
[root@server21 ~]# chkconfig --list |grep httpd                     // 可以通過chkconfig檢視
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

一、基於 xinetd 服務的管理
### --- 基於 xinetd 服務的啟動

~~~     我們使用 telnet 服務來舉例,telnet 服務是用來程序系統遠端管理的,埠時 23。
~~~     不過需要注意的是 telnet 的遠端管理資料在網路當中是明文傳輸,非常不安全。
~~~     所以我們在生產伺服器上是不建議啟動 telnet 服務的,我們這裡只是舉例而已。
~~~     在生成伺服器上,遠端管理使用的是 ssh 協議,ssh 是加密的更加安全。
### --- 在生產環境中堅決不可以安裝telnet;
### --- 安裝Telnet的服務端

[root@server21 ~]# rpm -ivh /mnt/cdrom/Packages/telnet-server-0.17-48.el6.x86_64.rpm
### --- chkconfig檢視是基於xinetd服務

[root@server21 ~]# chkconfig --list
    telnet:         off
[root@server21 ~]# vim /etc/xinetd.d/telnet 
# default: on
# description: The telnet server serves telnet sessions; it uses \
#       unencrypted username/password pairs for authentication.service telnet                                               // 服務的名稱為 telnet
service telnet
{
flags           = REUSE                                         // 標誌為 REUSE,設定 TCP/IP socket 可重用
socket_type     = stream                                        // 使用 TCP 協議資料包
wait            = no                                            // 允許多個連線同時連線
user            = root                                          // 啟動服務的使用者為 root
server          = /usr/sbin/in.telnetd                          // 服務的啟動程式
log_on_failure  += USERID                                       // 登陸失敗後,記錄使用者的 ID
disable         = yes                                           // 服務不啟動
}
[root@server21 ~]# vim /etc/xinetd.d/telnet 
~~~     修改配置檔案
service telnet
{
~~~     …省略部分輸出…
disable             = no                                        // 把 yes 改為 no 
}
### --- 啟動telnet,只能啟動xinetd服務。

[root@server21 ~]# service xinetd restart
[root@server21 ~]# chkconfig --list 
    telnet:         on  
二、基於 xientd 服務的自啟動
### --- 基於 xientd 服務的自啟動
~~~     使用 chkconfig 命令管理自啟動
~~~     使用 ntsysv 命令管理自啟動
~~~     基於 xinetd 的服務,沒有自己的執行級別,是依靠 xinetd 服務的執行級別。
~~~     所以不用指定--level 選項

[root@localhost ~]# chkconfig 服務名 on|off
三、獨立服務的啟動指令碼分析
### --- 獨立服務的啟動指令碼分析

~~~     既然獨立的服務啟動是依靠/etc/init.d/httpd 這個指令碼來進行啟動管理的,
~~~     那麼這個指令碼中到底是什麼樣子的?既然我們已經學習了 shell 指令碼,
~~~     那麼我們就來學習一下這個指令碼到底是怎麼實現apache 服務的管理的。
#!/bin/bash
# 
# httpd
# Startup script for the Apache HTTP Server
# 
#  chkconfig: - 85 15                                   // 下列兩句話不是註釋,而是chkconfig可以被呼叫
#  description: The Apache HTTP Server is an efficient and extensible \
# 自啟動設定 -代表自啟動級別,85(S85)代表啟動序號,15(K15)代表關閉序號。
# 
#   server implementing the current HTTP standards.
# 服務描述。以上兩行用於 apache 自啟動。
#  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
# 以上都是註釋。
# Source function library.
. /etc/rc.d/init.d/functions
# "."其實就是 source,就是呼叫 functions 檔案。
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# 判斷 httpd 如果是檔案,則呼叫 httpd 檔案。
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# 定義變數 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.
apachectl=/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 shutdown
#  when not running is also a failure.
So we just do it the way init scripts
#  are expected to behave here.
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
#  定義 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 檔案。
#  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() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
# 定義 stop 函式,用來關閉 apache 服務,關閉服務之後會刪除 pid 檔案。
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 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
# 定義 reload 函式,用於 apache 的重新載入。
# 通過/usr/sbin/httpd –t 命令判斷 apache 的配置檔案。如果配置檔案報錯,則輸出錯誤提示。
# 如果配置檔案正確,則重新載入 apache。
# See how we were called.
case "$1" in
# 判斷執行指令碼後的第一個引數的值,$1 表示執行指令碼時的第一個引數。
start)
start
;;
;;
# 如果引數值為 start,則呼叫 start 函式。
stop)
stop
;;
# 如果引數值為 stop,則呼叫 stop 函式。
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
# 如果引數值為 status,則執行 status –p $httpd 命令測試 apache 狀態。
restart)
stop
start
;;
# 如果引數值為 restart,則先呼叫 stop 函式,再呼叫 start 函式
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
# 如果引數值為 condrestart 或 try-restart,
# 則只有 apache 服務是已經執行時才先呼叫 stop 函式,再呼叫 start 函式,
# 重啟 apache。如果 apache 服務沒有執行,則不重啟 apache。
force-reload|reload)
reload
;;
# 如果引數值為 force-reload 或 reload,則呼叫 reload 函式。
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
# 如果引數是 graceful 或 help 或 configtest 或 fullstatus,
# 則執行/usr/sbin/apachectl 命令,並把引數作為命令的引數傳入 apachectl 命令。
*)
echo
$"Usage:
$prog
{start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|config
test}"
RETVAL=2
# 如果輸出的引數不是以上任何引數,則輸出錯誤資訊
esac
exit $RETVAL
# 通過這個指令碼,我們可以對 apache 服務的啟動有更深的瞭解了。








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)