概述:CentOS 5和6下的服务启动时,使用的命令都是service 服务名 start方式启动,其实service命令也是调用了/etc/init.d/下的脚本,下面请看具体步骤

实验步骤:

    首先准备好服务脚本,下面这一段代码是我之前写的,其类似于我们的服务启动时接受的参数,然后根据你所输入的参数来返回输出的内容

[root@localhost ~]# vim testsrv #!/bin/bash#function: script test#author: xiaoshui#[ -z $1 ] && echo "empty" &&exitdir=`basename $0`Dir="/var/lock/subsys/$dir"var=`echo $1 | tr 'A-Z' 'a-z'`case $var instart)        if [ ! -e "$Dir" ];then                touch $Dir && echo "$dir start success"        else                echo "$dir already start"        fi        ;;stop)        if [ ! -e "$Dir" ];then                echo "$dir is not start"        else                rm -f $Dir && echo "$dir stop success"        fi        ;;restart)        if [ ! -e "$Dir" ];then                echo "$dir not start"                touch $Dir && echo "$dir restart success"        else                rm $Dir && echo "$dir stopped"                touch $Dir && echo "$dir restart success"        fi        ;;status)        if [ ! -e "$Dir" ];then                echo "$dir is stopped"         else                echo "$dir is running"        fi        ;;*)        echo "argues error,please input start|restart|stop|status"        ;;esac

    第一步:将脚本拷贝至/etc/init.d/目录下,并在前面的#!/bin/bash下加上一行#chkconfig:35 12 88

[root@localhost ~]# vim /etc/init.d/testsrv #!/bin/bash#function: script test#chkconfig:35 12 88#description: test service#author: xiaoshui

    加上这内容的意义在于,35表示初始在哪个级别下启动,-表示默认都不启动 12 88 表示其表示/etc/rc.d/rc#.d/下面的K和S大头的文件,其含义就是启动的优先级。

    第二步:chkconfig --add 服务  将服务加到chkconfig列表中

[root@localhost rc.d]# chkconfig --add testsrv[root@localhost rc.d]# chkconfig --list ......省略........testsrv            0:off    1:off    2:off    3:on    4:off    5:on    6:off.......省略.....

    第三步:使用service命令进行测试

[root@localhost rc.d]# service testsrv starttestsrv start success[root@localhost rc.d]# service testsrv stoptestsrv stop success[root@localhost rc.d]# service testsrv restarttestsrv not starttestsrv restart success[root@localhost rc.d]# service testsrv statustestsrv is running[root@localhost rc.d]# service testsrv statfsdfsfargues error,please input start|restart|stop|status