chkconfig命令詳細介紹,chkconfig詳細介紹

來源:互聯網
上載者:User

chkconfig命令詳細介紹,chkconfig詳細介紹

命令介紹:

chkconfig命令用來更新、查詢、修改不同運行級上的系統服務。比如安裝了httpd服務,並且把啟動的指令碼放在了/etc/rc.d/init.d目錄下,有時候需要開機自動啟動它,而有時候則不需要,因此,就可以使chkconfig命令來進行控制,這個命令就相當於一個開關,不過這個開關有[0-6]共7個檔.

# 0 - 停機
# 1 - 單一使用者模式 
# 2 - 多使用者,沒有NFS 
# 3 - 完全多使用者模式(標準的運行級) 
# 4 - 沒有用到 
# 5 - X11(xwindow) 
# 6 - 重新啟動 

表示在不同層級下的運行狀態是on還是off。所以千萬不用講運行層級設定為0,6;最常用的就是2,3,5

chkconfig --list [name] 服務列表[可根據實際需要,停掉不用服務]
chkconfig --add  [name] 服務添加[如預設,則從預設的init指令碼自動建立] 
chkconfig --del  [name] 服務刪除[並把相關符號串連從/etc/rc[0-6].d刪除]
chkconfig --level name <on|off|reset>  
on    服務在改變運行級時的啟動
off   服務在改變運行級時的停止
reset 指初始化服務資訊
level 指運行層級;比如235表示運行層級為2、3、5,預設新增服務2、3、4、5

至於設定檔,可以放置到init的初始檔案中,也可以再shell指令碼中添加:

例1:random.init 包含三行:# chkconfig: 2345 20 80 # description: Saves and restores system entropy pool for \ # higher quality random number generation. 表明 random 指令碼應該在運行級 2, 3, 4, 5 啟動,啟動優先權為20,停止優先權為:80
例2: 設定檔寫在運行指令碼中[root@linux  ~]# cat /etc/init.d/test#!/bin/bash# chkconfig: 345 30 70# description: Test service# author: Jerry_1126     # version: v1.01
常用例子:

  • 例子1: 指令碼中檢查服務的啟動;

# vi check.shchkconfig network && echo "Network service is configured"chkconfig httpd   && echo "httpd service is configured"# ./check.shNetwork service is configured

NOTE:chkconfig直接加服務名,如果返回真的話,echo資訊。也可檢查運行層級

# vi check1.shchkconfig network --level 3 && echo "Network service is configured for level 3"chkconfig network --level 1 && echo "Network service is configured for level 1"# ./check1.shNetwork service is configured for level 3
  • 例子2: 檢查當前啟動並執行服務及層級
# chkconfig --listabrtd   0:off   1:off   2:off   3:on    4:off   5:on    6:offacpid   0:off   1:off   2:off   3:off   4:off   5:off   6:offatd     0:off   1:off   2:off   3:on    4:on    5:on    6:off...
如果只想查運行層級為3且開關開啟的,則可以:

chkconfig --list | grep 3:on
如果只想查看具體某個服務,則可以:

chkconfig --list | grep network
  • 例子3: 添加服務,自動會在2,3,4,5開啟
# chkconfig --list | grep iptables# chkconfig --add iptables# chkconfig --list | grep iptablesiptables       0:off   1:off   2:on    3:on    4:on    5:on    6:off
  • 例子4: 刪除服務
# chkconfig --list | grep ip6tablesip6tables       0:off   1:off   2:off   3:on   4:off   5:off   6:off# chkconfig --del iptables# chkconfig --list | grep iptables
  • 例子5: 開啟、關閉運行層級的服務
# chkconfig --level 5 mysql off        # 在運行層級為5的開關上,關閉mysql服務
# chkconfig --level 235 mysql on       # 在運行層級為2,3,5開關上,開啟的mysql服務
  • 例子6: 檢查rc.d子指令碼下的指令檔
# chkconfig --list | grep xinetdxinetd                    0:off  1:off  2:off  3:on   4:off  5:on   6:offxinetd based services:# cd /etc/rc.d/rc3.d# ls | grep xinetdK08xinetd             #關閉的時候,殺掉K開頭的檔案S14xinetd             #啟動的時候,啟動S開頭的檔案
  • 例子7: 執行添加命令時,rc.d目錄下指令碼變化
假如nfsserver沒啟動,那麼在/etc/rc.d/rc*.d目錄下,不存在檔案

# chkconfig  --list | grep nfsservernfsserver                 0:off  1:off  2:off  3:off  4:off  5:off  6:off# ls /etc/rc.d/rc3.d | grep nfsserver# ls /etc/rc.d/rc5.d | grep nfsserver
假如nfsserver服務啟動後,目錄變化:

# chkconfig --add nfsservernfsserver                 0:off  1:off  2:off  3:on   4:off  5:on   6:off# cd  /etc/rc.d/rc3.d# ls -l | grep nfsserverlrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserverlrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver# cd /etc/rc.d/rc5.d# ls -l | grep nfsserverlrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserverlrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

假如nfsserver服務關閉後,目錄變化:

# chkconfig --level 5 nfsserver off# ls /etc/rc.d/rc5.d  | grep nfsserver

自訂服務添加:

舉個簡單例子,說明下一個自訂服務添加的詳細過程:

[root@linux  init.d]# cd /etc/init.d/test             #進入目錄[root@linux  init.d]# touch test                      #在該目錄下,建立個服務指令碼[root@linux  init.d]# cat test                        #指令碼內容#!/bin/bash# chkconfig: 345 30 70          #此行必須有,運行層級3,4,5,啟動時許可權30,關閉時許可權70# description: Test Service Running difference level# author: Jerry_1126     # version: v1.01case "$1" in    stop) echo -e "The Test service is ${1}ed! \n"       ;;   start) echo -e "The Test service is ${1}ed! \n"       ;; restart) echo -e "The Test service is restart! \n"       ;;       *) echo -e "The parameter is wrong! \n"       ;;esac[root@linux  init.d]# chmod +x ./test[root@linux  init.d]# chkconfig --add test[root@linux  init.d]# service test startThe Test service is started![root@linux  init.d]# chkconfig --list | grep testtest            0:off   1:off   2:on    3:on    4:on    5:on    6:off[root@linux  init.d]# chkconfig --level 3 test on[root@linux  init.d]# chkconfig --list | grep testtest            0:off   1:off   2:off   3:on    4:off   5:off   6:off
附錄:

附1:常用服務介紹

amd:           # 自動安裝網路檔案系統守侯進程apmd:           # 進階電源管理Arpwatch:      # 記錄日誌並構建一個在LAN介面上看到的乙太網路地址和IP地址對資料庫Autofs:        # 自動安裝管理進程automount,與NFS相關,依賴於NISBootparamd:    # 引導參數伺服器,為LAN上的無盤工作站提供引導所需的相關資訊crond:         # 計劃任務Dhcpd:         # 啟動一個動態IP地址分配伺服器Gated:         # 網關路由守候進程,使用動態OSPF路由選擇協議Httpd:         # WEB伺服器Inetd:         # 支援多種網路服務的核心守候程式Innd:          # Usenet新聞伺服器Linuxconf:     # 允許使用本地WEB伺服器作為使用者介面來配置機器Lpd:           # 列印伺服器Mars-nwe:      # mars-nwe檔案和用於Novell的列印伺服器Mcserv:        # Midnight命令檔案伺服器named:         # DNS伺服器netfs:         # 安裝NFS、Samba和NetWare網路檔案系統network:       # 啟用已配置網路介面的指令碼程式nfs:           # 開啟NFS服務nscd:          # nscd伺服器,用於NIS一個支援服務,它快取使用者口令和組成成員關係portmap:       # RPC portmap管理器,與inetd類似,它管理基於RPC服務的串連postgresql:    # 一種SQL資料庫伺服器。routed:        # 路由守候進程,使用動態RIP路由選擇協議rstatd:        # 一個為LAN上的其它機器收集和提供系統資訊的守候程式ruserd:        # 這是一個基於RPC的服務,它提供關於目前記錄到LAN上一個機器日誌中的使用者資訊rwalld:        # 這是一項基於RPC的服務,允許使用者給每個註冊到LAN機器的其他終端寫訊息rwhod:         # 啟用rwhod服務進程,它支援LAN的rwho和ruptime服務sendmail:      # 郵件伺服器sendmailsmb:           # Samba檔案分享權限設定/列印服務snmpd:         # 本地簡單網路管理候進程squid:         # 啟用Proxy 伺服器squidsyslog:        # 一個讓系統引導時起動syslog和klogd系統日誌守候進程的指令碼xfs:           # X Window字型伺服器,為本地和遠程X伺服器提供字型集xntpd:         # 網路時間伺服器ypbind:        # 為NIS(網路資訊系統)客戶機啟用ypbind服務進程yppasswdd:     # NIS口令伺服器ypserv:        # NIS主伺服器gpm:           # 管滑鼠的服務identd:        # AUTH服務,在提供使用者資訊方面與finger類似

附2:HTTP Service的完整指令碼

#!/bin/bash## httpd        Startup script for the Apache HTTP Server## chkconfig: - 85 15# description: Apache is a World Wide Web server.  It is used to serve \#              HTML files and CGI.# processname: httpd# config: /etc/httpd/conf/httpd.conf# config: /etc/sysconfig/httpd# pidfile: /var/run/httpd.pid# Source function library.. /etc/rc.d/init.d/functionsif [ -f /etc/sysconfig/httpd ]; then        . /etc/sysconfig/httpdfi# 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.apachectl=/usr/sbin/apachectlhttpd=${HTTPD-/usr/sbin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0STOP_TIMEOUT=${STOP_TIMEOUT-10}# check for 1.3 configurationcheck13 () {        CONFFILE=/etc/httpd/conf/httpd.conf        GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"        GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"        GONE="${GONE}AccessConfig|ResourceConfig)"        if LANG=C grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then                echo                echo 1>&2 " Apache 1.3 configuration directives found"                echo 1>&2 " please read /usr/share/doc/httpd-2.2.3/migration.html"                failure "Apache 1.3 config directives test"                echo                exit 1        fi}# 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: "        check13 || exit 1        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() {        echo -n $"Stopping $prog: "        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd        RETVAL=$?        echo        [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}}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}# See how we were called.case "$1" in  start)        start        ;;  stop)        stop        ;;  status)        status -p ${pidfile} $httpd        RETVAL=$?        ;;  restart)        stop        start        ;;  condrestart|try-restart)        if status -p ${pidfile} $httpd >&/dev/null; then                stop                start        fi        ;;  force-reload|reload)        reload        ;;  graceful|help|configtest|fullstatus)        $apachectl $@        RETVAL=$?        ;;  *)        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"        RETVAL=2esac



我的linux系統沒有chkconfig命令怎辦?

你的是什麼系統。Redhat的話,光碟片裡有chkconfig的RPM。或者用下面的地址
mirrors.163.com/...64.rpm
 
對於Linux命令 chkconfig問題

chckcongfig httpd on 是設定httpd每次開機自己啟動,但並不現在就啟動
service httpd start 是現在啟動httpd服務,但是下次開機不會自動啟動
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.