chkconfig命令详细介绍

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Jerry_1126/article/details/38684201

命令介绍:

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:    # 引导参数服务器,为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:      # 邮件服务器sendmail
smb:           # Samba文件共享/打印服务
snmpd:         # 本地简单网络管理候进程
squid:         # 激活代理服务器squid
syslog:        # 一个让系统引导时起动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/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


展开阅读全文

没有更多推荐了,返回首页