zoukankan      html  css  js  c++  java
  • init.d functions的daemon函数

    daemon函数说明

    # 该函数的作用是启动一个可执行的二进制程序:
    # 使用方法:
    # 1、daemon {--check program|--check=program} [--user username|--user=username] [--pidfile pidfile|--# pidfile=pidfile] [+/- nice]
    # 2、daemon [--user username|--user=username] [--pidfile pidfile|--pidfile=pidfile] [+/- nice] {program}
    # [program options]
    # 说明:
    # 调用该函数时:
    # 方法1:可以使用--check 来指定可运行二进制程序;
    # 方法2:还可以在以破折号开头的参数后直接跟二进制程序.
    # 但是,如果二进制程序运行时一定要带一些参数的话,最好使用方法2.
    # 注意:
    #     如果调用函数 daemon 启动一个二进制可执行程序时。没有指定pidfile文件所在的位# 置。有可能会造成程序的再次启动。因为,函数默认是到/var/run/目录下找一个进程的进程
    # 号的。如果找不到就再次启动二进制程序。
    # A function to start a program.
    daemon() {
            # Test syntax.
            local gotbase= force= nicelevel corelimit  
            # 设置局部变量,用来保存调用该函数传递过来的参数
            local pid base= user= nice= bg= pid_file=
            nicelevel=0
            while [ "$1" != "${1##[-+]}" ]; do  
    # 该循环条件作用是:找出哪个参数是可执行程序。循环到可执行程序参数就结束while循环。
    # 因为其它参数前面都有破折号标志(--),符合while循环条件.如:---user 或 --pidfile
    # 所以调用该函数时:如果指定了运行该二进制程序的用户是谁--user 或 运行程序的pid文件# 等这些选项参数,调用该函数时,一定要把这些参数放在二进制可运行程序参数前。
    # 由于可运行程序这个参数没有破折号标识,当while循环到该参数时,就结束循环。这带来一个
    # 好处:有些二进制可运行程序运行时要带选项才能运行。就可以在二进制可运行程序后面添加
    # 二进制执行程序的选项。
    #    如:想运行nrpe这服务: 要指定配置文件和是以超级守护进程方式还是独立守护进程方式。
    # daemon --pidfie=/var/run/nrped.pid /usr/local/nagios/bin/nrpe -c /etc/nagios/nrpe.conf -d
    # 选项一般要使用破折号标明。
                
        case $1 in   
    #下面的case是检测二进制执行程序前的其它参数的。把符合条件的参数使用前面定义的变量记录
    #下来
                '')    echo $"$0: Usage: daemon [+/-nicelevel] {program}"
                       return 1;;
                --check)
                       base=$2
                       gotbase="yes"
                       shift 2
                       ;;
                --check=?*)
                       base=${1#--check=}
                       gotbase="yes"
                       shift
                       ;;
                --user)
                       user=$2
                       shift 2
                       ;;
                --user=?*)
                       user=${1#--user=}
                       shift
                       ;;
                --pidfile)
                       pid_file=$2
                       shift 2
                       ;;
                --pidfile=?*)
                       pid_file=${1#--pidfile=}
                       shift
                       ;;
                --force)
                       force="force"
                       shift
                       ;;
                [-+][0-9]*)
                       nice="nice -n $1"
                       shift
                       ;;
                *)     echo $"$0: Usage: daemon [+/-nicelevel] {program}"
                       return 1;;
              esac
            done
            
    #####################################################################
            # Save basename.
            [ -z "$gotbase" ] && base=${1##*/} 
    # 如果没有使用--check来指定二进制执行程序就执行 && 后面的参数替换。找出二进制执行
    # 程序并保存在变量里。                            
            # See if it's already running. Look *only* at the pid file.
            __pids_var_run "$base" "$pid_file"  # 判断该二进制程序是否运行的。
            [ -n "$pid" -a -z "$force" ] && return 
            # 如果找到进程的进程号, 直接返回。就不往下执行了。
            # make sure it doesn't core dump anywhere unless requested           # 资源限定
            corelimit="ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}"
            # if they set NICELEVEL in /etc/sysconfig/foo, honor it
            [ -n "${NICELEVEL:-}" ] && nice="nice -n $NICELEVEL"
            # Echo daemon     
            [ "${BOOTUP:-}" = "verbose" -a -z "${LSB:-}" ] && echo -n " $base"
            # And start it up. # 如果bash shell 执行到这,就启动二进制程序。
            if [ -z "$user" ]; then   
               $nice /bin/bash -c "$corelimit >/dev/null 2>&1 ; $*"  
               # 启动该二进制程序
            else
               $nice runuser -s /bin/bash - $user -c "$corelimit >/dev/null 2>&1 ; $*"         
               #如果调用该函数时指定了--user就使用该方式启动启                                                                               
            fi
            [ "$?" -eq 0 ] && success $"$base startup" || failure $"$base startup" 
            #根据 if 语句的执行结果$?来判断启动二进制程序是否成功
           #启动成功就调用函数:success 反之则调用函数:failure
    }
  • 相关阅读:
    Windows2012 cannot access netapp CIFS share
    Import SHA2 SSL cert to Windows IIS7
    IE11登陆交行网银崩溃
    Understanding and Managing SMTP Virtual Servers
    IIS SMTP Queue stuck
    C#夯实基础之多线程三:线程的优先级
    C#夯实基础之多线程二:主线程、前台线程与后台线程
    ORA-00257: archiver error. Connect internal only, until freed.
    C#夯实基础之多线程一:初识多线程
    在Oracle中恢复被DROP掉的表
  • 原文地址:https://www.cnblogs.com/milton/p/6212595.html
Copyright © 2011-2022 走看看