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.sh
chkconfig network && echo "Network service is configured"
chkconfig httpd   && echo "httpd service is configured"

# ./check.sh
Network service is configured

NOTE:chkconfig直接加服务名,假设返回真的话,echo信息。也可检查执行级别

# vi check1.sh
chkconfig network --level 3 && echo "Network service is configured for level 3"
chkconfig network --level 1 && echo "Network service is configured for level 1"

# ./check1.sh
Network service is configured for level 3
  • 样例2: 检查当前执行的服务及级别
# chkconfig --list
abrtd   0:off   1:off   2:off   3:on    4:off   5:on    6:off
acpid   0:off   1:off   2:off   3:off   4:off   5:off   6:off
atd     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 iptables
iptables       0:off   1:off   2:on    3:on    4:on    5:on    6:off
  • 样例4: 删除服务
# chkconfig --list | grep ip6tables
ip6tables       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 xinetd
xinetd                    0:off  1:off  2:off  3:on   4:off  5:on   6:off
xinetd based services:

# cd /etc/rc.d/rc3.d
# ls | grep xinetd
K08xinetd             #关闭的时候,杀掉K开头的文件
S14xinetd             #启动的时候。启动S开头的文件
  • 样例7: 运行加入命令时,rc.d文件夹下脚本变化
假如nfsserver没启动。那么在/etc/rc.d/rc*.d文件夹下,不存在文件

# chkconfig  --list | grep nfsserver
nfsserver                 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 nfsserver
nfsserver                 0:off  1:off  2:off  3:on   4:off  5:on   6:off

# cd  /etc/rc.d/rc3.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 S14nfsserver -> ../nfsserver

# cd /etc/rc.d/rc5.d
# ls -l | grep nfsserver
lrwxrwxrwx 1 root root 12 2011-06-18 00:52 K08nfsserver -> ../nfsserver
lrwxrwxrwx 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.01

case "$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 start
The Test service is started!

[root@linux  init.d]# chkconfig --list | grep test
test            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 test
test            0:off   1:off   2:off   3:on    4:off   5:off   6:off
附录:

附1:经常使用服务介绍

amd:           # 自己主动安装网络文件系统守侯进程
apmd:           # 高级电源管理
Arpwatch:      # 记录日志并构建一个在LAN接口上看到的以太网地址和IP地址对数据库
Autofs:        # 自己主动安装管理进程automount。与NFS相关,依赖于NIS
Bootparamd:    # 引导參数server。为LAN上的无盘工作站提供引导所需的相关信息
crond:         # 计划任务
Dhcpd:         # 启动一个动态IP地址分配server
Gated:         # 网关路由守候进程,使用动态的OSPF路由选择协议
Httpd:         # WEBserver
Inetd:         # 支持多种网络服务的核心守候程序
Innd:          # Usenet新闻server
Linuxconf:     # 同意使用本地WEBserver作为用户接口来配置机器
Lpd:           # 打印server
Mars-nwe:      # mars-nwe文件和用于Novell的打印server
Mcserv:        # Midnight命令文件server
named:         # DNSserver
netfs:         # 安装NFS、Samba和NetWare网络文件系统
network:       # 激活已配置网络接口的脚本程序
nfs:           # 打开NFS服务
nscd:          # nscdserver,用于NIS一个支持服务,它快速缓存用户口令和组成成员关系
portmap:       # RPC portmap管理器,与inetd相似,它管理基于RPC服务的连接
postgresql:    # 一种SQL数据库server。
routed:        # 路由守候进程,使用动态RIP路由选择协议
rstatd:        # 一个为LAN上的其他机器收集和提供系统信息的守候程序
ruserd:        # 这是一个基于RPC的服务。它提供关于当前记录到LAN上一个机器日志中的用户信息
rwalld:        # 这是一项基于RPC的服务。同意用户给每一个注冊到LAN机器的其他终端写消息
rwhod:         # 激活rwhod服务进程。它支持LAN的rwho和ruptime服务
sendmail:      # 邮件serversendmail
smb:           # Samba文件共享/打印服务
snmpd:         # 本地简单网络管理候进程
squid:         # 激活代理serversquid
syslog:        # 一个让系统引导时起动syslog和klogd系统日志守候进程的脚本
xfs:           # X Window字型server,为本地和远程Xserver提供字型集
xntpd:         # 网络时间server
ypbind:        # 为NIS(网络信息系统)客户机激活ypbind服务进程
yppasswdd:     # NIS口令server
ypserv:        # NIS主server
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/functions


if [ -f /etc/sysconfig/httpd ]; then
        . /etc/sysconfig/httpd
fi


# 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/apachectl
httpd=${HTTPD-/usr/sbin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}


# check for 1.3 configuration
check13 () {
        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|hel p|configtest}" RETVAL=2 esac



posted @ 2017-08-11 15:09  jhcelue  阅读(2179)  评论(0编辑  收藏  举报