shell脚本--制作自己的服务脚本

首先注意一下,我用的环境是centos6.5,中间有一些操作和在Ubuntu上有一些地方的操作是不同的,

编写脚本

首先看一个实例:假设有一个test的服务,可以通过命令对test进行启动、关闭或者重启,下面这个脚本就模拟这个功能:

#!/bin/bash
#test.sh

case $1 in
    start)
        echo "starting service......"
        sleep 1
        echo "started the service!!"
        ;;
    stop)
        echo "stopping service......"
        sleep 1
        echo "stopped the service!!"
        ;;
    restart)
        echo "restarting service......"
        sleep 1
        echo "restarted the service!!"
        ;;
    *)
        echo "warning: invalid option -> ${1}"
        ;;
esac

  运行这个脚本,结果如下

[root@localhost ~]# ./test.sh start
starting service......
started the service!!
[root@localhost ~]# ./test.sh stop
stopping service......
stopped the service!!
[root@localhost ~]# ./test.sh retart
warning: invalid option -> retart
[root@localhost ~]# ./test.sh restart
restarting service......
restarted the service!!
[root@localhost ~]# 

  

将脚本设置为自启动:

  这一步需要将运行脚本的命令写在一个文件中,这个文件有点特殊:是系统启动完系统的进程后,然后会执行这个脚本,用来启动脚本中指定的服务。这个脚本的路径是在CentOS和RHEL的/etc/rc.d/rc.local,打开这个文件,即可看到文件头部的提示信息:

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

  默认这个文件是空的,但是现在我们想要将自己的test服务设置为自启动,那么就可以将命令写在这个脚本中,要注意的是,脚本的路径要写完整路径(绝对路径),我在其中添加的内容是:

/home/root/test.sh start

  可以看到,这个只是将启动的命令写在这里,如果有需要,可以将一些服务的其他命令也写在这里,比如关闭apache,mysql。。。。这些都是可以的,这个脚本并不是说只能写开启服务命令,这个脚本只是会在 执行完其他所有的初始化脚本时,才会执行这个脚本。

  然后使用init 6来重启一下Linux系统,如果你是安转的纯字符界面的Linux,那么你就可以观察到,启动时,会在最后的时候出现test.sh start一行,并且会暂停一秒,因为test.sh中用sleep 1这条命令。如果嫌弃后面加一个.sh的后缀难看,可以将文件名的.sh后缀去掉,rc.local中直接写/home/root/test start即可。

 

使用service启动服务

  咱们通常使用的apache,msyql的启动使用如下命令:

service httpd start
service mysql start

  那么也可以使用service命令来启动咱们自己的服务,但是前提是要将启动脚本移动到/etc/rc.d/init.d/这个目录下,如果不移动的话,service去/etc/rc.d/init.d/目录下查找不到test脚本,就会出错,为了方便和好看,这里将test.sh重命名为test,去掉后缀,好看一点吧:

[root@localhost ~]# service test.sh start   #为移动之前,尝试使用service启动,会失败
test.sh: unrecognized service
[root@localhost ~]# cp test.sh /etc/rc.d/init.d/test
[root@localhost ~]# service test start
starting service......
started the service!!
[root@localhost ~]# service test stop
stopping service......
stopped the service!!

  到这里,已经完成了使用service控制服务了,但是还有一个问题:不能使用chkconfig来设置开机启动。

 

使用chkconfig设置开机启动:

  现在test服务是不能通过chkconfig来设置开机启动,要想知道原因很简单,只需要查看一个可以通过chkconfig来设置开机启动的脚本就行了,比如apache服务,他的启动脚本是/etc/rc.d/init.d/httpd。

查看一下:

#!/bin/sh
# Startup script for the Apache Web Server
# chkconfig: 345 85 15

  上面的脚本中,虽然都是注释,但是,第3行才是他们可以使用chkconfig来设置开机启动的关键点,有了这一行,才能实现目的。首先看一下第3行chkconfig后面的三个数字代表什么意思。

  chkconfig: A B C

    其中A表示的是在哪一级启动,apache的httpd脚本中是在3,4,5级中启动,即让rc3.d、rc4.d,rc5.d生效。

    B表示启动的顺序:在系统启动的时候,有那么多的服务要启动,这个数字则表示他的启动顺序,数字越大越晚启动,建议不重要的服务都放在后期启动

    C表示关闭的顺序:在系统关闭的时候,有那么多的服务要关闭,这个数字则表示他的关闭顺序,数字越小越先关闭,建议不重的服务先关闭

  B和C的开启顺序可以在rc1.d-rc5.d这几个文件中查看,就可以看到他的启动顺序,以S开头,表示start,后面一个数字表示启动顺序,然后是他的服务脚本名称。

  而关闭的顺序可以再rc6.d中查看到的他的关闭书序,就可以看到他的关闭顺序,以K开头,表示kill,后面一个数字表示关闭顺序,然后是他的服务脚本名称。

[root@localhost ~]# ls /etc/rc3.d | grep httpd
S85httpd
[root@localhost ~]# ls /etc/rc6.d | grep httpd
K15httpd

  

  好了,扯远了,所以我们在test脚本中也添加chkconfig这一行

#!/bin/bash
#test.sh

# chkconfig: 345 90 10

case $1 in
    start)
        echo "starting service......"
        sleep 1
        echo "started the service!!"
        ;;
    stop)
        echo "stopping service......"
        sleep 1
        echo "stopped the service!!"
        ;;
    restart)
        echo "restarting service......"
        sleep 1
        echo "restarted the service!!"
        ;;
    *)
        echo "warning: invalid option -> ${1}"
        ;;
esac

  然后运行:

[root@localhost rc3.d]# chkconfig --add test
[root@localhost rc3.d]# chkconfig test on
[root@localhost rc3.d]# chkconfig --list test
test           	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@localhost rc3.d]# chkconfig test off
[root@localhost rc3.d]# chkconfig --list test
test           	0:off	1:off	2:off	3:off	4:off	5:off	6:off

  这样就可以看到结果了,完美!

因为已经可以通过chkconfig来设置开机启动,那么就不用在/etc/rc.local添加命令来启动test服务了,于是可以将前面示例中的/home/root/test.sh start那一行命令删掉

  

posted @ 2018-02-03 19:58  寻觅beyond  阅读(1656)  评论(0编辑  收藏  举报
返回顶部