• 运行级别脚本2


    上一篇《运行级别脚本》主要是从概念上认知运行级别脚本。本篇主要记录一些实用性的知识。

    一、运行级别脚本的结构

    [root@localhost rc0.d]# cat /etc/init.d/ntpd
    #!/bin/bash
    #
    # ntpd          This shell script takes care of starting and stopping
    #               ntpd (NTPv4 daemon).
    #
    #下面这句中的-表示默认不启动,58和74分别表示系统启动和退出时启动、关闭服务的优先级
    # chkconfig: - 58 74
    #以下为运行级别脚本的描述信息
    # description: ntpd is the NTPv4 daemon. 
    # The Network Time Protocol (NTP) is used to synchronize the time of 
    # a computer client or server to another server or reference time source, 
    # such as a radio or satellite receiver or modem.
    
    #省略部分为读取配置文件相关内容
    ......
    
    #定义启动函数start
    start() {
            # Check that networking is up.
            [ "$NETWORKING" = "no" ] && exit 1
    
            readconf;
    
            if [ -n "$dostep" ]; then
                echo -n $"$prog: Synchronizing with time server: "
                /usr/sbin/ntpdate $dropstr -s -b $NTPDATE_OPTIONS $tickers &>/dev/null
                RETVAL=$?
                [ $RETVAL -eq 0 ] && success || failure
                echo
                if [ $RETVAL -eq 0 ]; then
                    [ "$SYNC_HWCLOCK" = "yes" ] && sync_hwclock
                else
                    OPTIONS="$OPTIONS -g"
                fi
            else
                # -g can replace the grep for time servers
                # as it permits ntpd to violate its 1000s limit once.
                OPTIONS="$OPTIONS -g"
            fi
            # Start daemons.
            echo -n $"Starting $prog: "
            daemon ntpd $OPTIONS
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && touch /var/lock/subsys/ntpd
            return $RETVAL
    }
    
    #定义停止服务函数stop
    stop() {
            echo -n $"Shutting down $prog: "
            killproc ntpd
            RETVAL=$?
            echo
            [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/ntpd
            return $RETVAL
    }
    
    #下面是判断参数1的结构
    # See how we were called.
    case "$1" in
      start)
            start
            ;;
      stop)
            stop
            ;;
      status)
            status ntpd
            RETVAL=$?
            ;;
      restart|reload)
            stop
            start
            RETVAL=$?
            ;;
      condrestart)
            if [ -f /var/lock/subsys/ntpd ]; then
                stop
                start
                RETVAL=$?
            fi
            ;;
    #如果参数1不是以上参数,则输出提示信息
      *)
            echo $"Usage: $0 {start|stop|restart|condrestart|status}"
            RETVAL=3
    esac
    
    #设置退出状态并退出
    exit $RETVAL

    这是一个非常长的脚本,上面仅截取了该脚本的一部分,此部分正好说明了运行级别脚本的编写方式。

    (1)首先需要以注释的方式声明服务默认启动的运行级别列表,系统启动、关闭时服务启动、关闭的优先级。

    (2)通常将服务的启动、关闭操作都写在函数中,然后以函数的方式调用。

    (3)使用case语句处理strart、stop等参数。

    (4)在case语句最后处理不合适的参数。

    (5)如果需要检查服务是否处于运行状态,最好创建运行标记文件。

    注意:设置运行级别脚本启动、关闭的优先级时,一定要注意其依赖的其他服务的优先级,以免启动服务失败。

    二、编写运行级别脚本

    编写运行级别脚本的目的可能是为自己编写的监控脚本添加新的启动方式,也可能是为某个产品编写启动服务等。此处引入一个示例脚本(并无实质性内容,在实际编写时需要在函数中添加实际的启动、关闭服务的语句):

    [root@localhost shell]# cat test
    #!/bin/bash
    
    #定义服务启动的运行级别为3、45,启动和关闭的优先级分别为80、10
    # chkconfig:345 80 10
    #以下这句是服务的描述信息
    # description:This is a test service
    
    #This is a test script
    #2013.12.20
    
    #定义参数错误的提示消息函数usage
    function usage()
    {
            echo "Usage:$0{start|stop|restart|reload}"
            return 0
    }
    
    #定义启动服务的函数start
    function start()
    {
            echo "Starting $0:"
            return 0
    }
    
    #定义关闭服务的函数stop
    function stop()
    {
            echo "Shutting down $0:"
            return 0
    }
    
    #脚本的主体部分
    #使用case语句判断参数的值
    case $1 in
            start)
                    start
                    ;;
            stop)
                    stop
                    ;;
            restart|reload)
                    stop
                    start
                    ;;
            *)
                    usage
                    exit 1
    esac
    exit 0

    注意:“# chkconfig”和“# description”开头的这两行,这是运行级别脚本中必须定义的内容,主要用于添加服务时初始化配置和描述信息。

    三、添加和管理运行级别脚本

    编写运行级别脚本的目的是能够将脚本作为服务添加到系统中。

    1、添加运行级别脚本为系统服务

    将运行级别脚本添加为系统服务之前,应该将编写完成的运行级别脚本复制到目录/etc/init.d中,并添加相应的执行权限。完成之后,就可以使用chkconfig命令将运行级别脚本添加为系统服务了。

    将示例脚本test添加为系统服务使用如下命令:

    [root@localhost shell]# chkconfig --add test

    添加成功后,命令不会有任何返回信息。可以使用以下命令查看添加到服务:

    [root@localhost shell]# chkconfig --list test
    test            0:off   1:off   2:off   3:on    4:on    5:on    6:off
    [root@localhost shell]# chkconfig --list |  grep test
    test            0:off   1:off   2:off   3:on    4:on    5:on    6:off

    另外,使用chkconfig –add test命令添加test为系统服务成功后,我们可以发现各个运行级别目录里已经自动添加了test的相关启动、关闭的脚本,并且这些脚本都是/etc/init.d/test的链接文件:

    [root@localhost shell]# ls -al /etc/rc.d/rc0.d | grep test
    lrwxrwxrwx  1 root root   14 Dec 20 20:08 K10test -> ../init.d/test
    [root@localhost shell]# ls -al /etc/rc.d/rc1.d | grep test
    lrwxrwxrwx  1 root root   14 Dec 20 20:08 K10test -> ../init.d/test
    [root@localhost shell]# ls -al /etc/rc.d/rc2.d | grep test
    lrwxrwxrwx  1 root root   14 Dec 20 20:08 K10test -> ../init.d/test
    [root@localhost shell]# ls -al /etc/rc.d/rc3.d | grep test
    lrwxrwxrwx  1 root root   14 Dec 20 20:08 S80test -> ../init.d/test
    [root@localhost shell]# ls -al /etc/rc.d/rc4.d | grep test
    lrwxrwxrwx  1 root root   14 Dec 20 20:08 S80test -> ../init.d/test
    [root@localhost shell]# ls -al /etc/rc.d/rc5.d | grep test
    lrwxrwxrwx  1 root root   14 Dec 20 20:08 S80test -> ../init.d/test
    [root@localhost shell]# ls -al /etc/rc.d/rc6.d | grep test
    lrwxrwxrwx  1 root root   14 Dec 20 20:08 K10test -> ../init.d/test

    2、管理使用运行级别脚本添加到服务

    使用chkconfig命令添加服务之后,就可以使用命令service启动服务:

    [root@localhost shell]# service test start
    Starting /etc/init.d/test:

    当然也可以随时删除:

    [root@localhost shell]# chkconfig --del test
  • 相关阅读:
    归并排序处理复杂对象例子
    Java归并排序的递归与非递归实现
    Java实现一个双向链表的倒置功能
    Node<T> 的作用
    Tomcat控制台总是打印日志问题的解决办法
    git回滚部分文件到某个版本
    ios-deploy was not found
    Ionic3的http请求如何实现token验证,并且超时返回登录页
    Ionic开发遇到的坑整理
    使用gradle命令代替CUBA Studio,启动项目
  • 原文地址:https://www.cnblogs.com/nufangrensheng/p/3484443.html
Copyright © 2020-2023  润新知