linux的functions之daemon函数详解

# 该函数的做用是启动一个可执行的二进制程序:ios

# 使用方法:web

一、daemon {--check program|--check=program} [--user username|--user=username] [--pidfile pidfile|--pidfile=pidfile] [+/- nice]shell

二、daemon [--user username|--user=username] [--pidfile pidfile|--pidfile=pidfile] [+/- nice] {program} [program options]bash

说明:less

    调用该函数时:ide

    方法1:能够使用--check 来指定可运行二进制程序;函数

    方法2:还能够在以破折号开头的参数后直接跟二进制程序.spa

     可是,若是二进制程序运行时必定要带一些参数的话,最好使用方法2.orm

注意:blog

    若是调用函数 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}


https://blog.51cto.com/9528du/1420058

相关文章
相关标签/搜索