调用'/etc/init.d/daemon stop'时杀死进程之前bash init.d脚本中的宽限期

时间:2011-06-23 09:47:39

标签: linux bash init.d

我正在为我的某个公司应用修改init.d脚本。显然我的bash脚本foo不够强大。

一旦Launcher看到Dispatcher已经终止,它就会退出。停止应用程序时,vanilla脚本始终显示[ FAILED ],因为Launcher在此脚本尝试终止之前已终止。

我已尝试添加2秒宽限期(充足),以便Launcher在尝试杀死之前终止,如果它仍在运行。

我有以下stop()函数:

#!/bin/bash
# chkconfig: 345 85 60
# description: .
# processname: xxx
# pidfile: /var/run/xxx.pid

# Source function library.
. /etc/rc.d/init.d/functions

RETVAL=0

PID_PATH=/tmp/
PID_PREFIX=xxx
LOCK_PATH=/tmp/

PIDFILE_D=${PID_PATH}${PID_PREFIX}_dispatcher.pid
PIDFILE_L=${PID_PATH}${PID_PREFIX}_launcher.pid
PIDFILE_J=${PID_PATH}${PID_PREFIX}_jobselector.pid
LOCK=${LOCK_PATH}${PID_PREFIX}

BASEPATH=`dirname $0`
echo $BASEPATH | grep -q "^/" && BASEPATH=`dirname $BASEPATH` || BASEPATH=$PWD/`dirname $BASEPATH`

# -- snip ---

stop() {
        echo -n $"Shutting down Dispatcher: "
        killproc -p $PIDFILE_D
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $PIDFILE_D

        echo -n $"Shutting down Launcher: "

        # launcher self terminates once it sees the dispatcher is not running
        # grace period of 2 seconds before explicitly killing it
        if [ -e "$PIDFILE_L" ]; then
            local i pid
            read pid < "$PIDFILE_L"
            for i in 1 2; do
                if checkpid $pid 2>&1; then
                    sleep 1
                fi
            done

            # if launcher still active after grace period, kill it
            if checkpid $pid 2>&1; then
                killproc -p $PIDFILE_L
                RETVAL=$?
            else
                success $"$base shutdown"
            fi
        else
            success $"$base shutdown"
        fi
        [ $RETVAL -eq 0 ] && rm -f $PIDFILE_L

        echo -n $"Shutting down Job Selector: "
        killproc -p $PIDFILE_J
        RETVAL=$?
        echo
        [ $RETVAL -eq 0 ] && rm -f $PIDFILE_J

        [ $RETVAL -eq 0 ] && rm -f $LOCK
        return $RETVAL
}

当我停止程序时,它不再显示“关闭启动器:...”行。

$ sudo ./bin/program stop
Shutting down Dispatcher:                             [  OK  ]
Shutting down Job Selector:                           [  OK  ]

支持功能:

killproc() {
        local RC killlevel= base pid pid_file= delay

        RC=0; delay=3
        # Test syntax.
        if [ "$#" -eq 0 ]; then
                echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
                return 1
        fi
        if [ "$1" = "-p" ]; then
                pid_file=$2
                shift 2
        fi
        if [ "$1" = "-d" ]; then
                delay=$2
                shift 2
        fi

        # check for second arg to be kill level
        [ -n "${2:-}" ] && killlevel=$2

        # Save basename.
        base=${1##*/}

        # Find pid.
        __pids_var_run "$1" "$pid_file"
        if [ -z "$pid_file" -a -z "$pid" ]; then
                pid="$(__pids_pidof "$1")"
        fi

        # Kill it.
        if [ -n "$pid" ] ; then
                [ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
                if [ -z "$killlevel" ] ; then
                       if checkpid $pid 2>&1; then
                           # TERM first, then KILL if not dead
                           kill -TERM $pid >/dev/null 2>&1
                           usleep 100000
                           if checkpid $pid && sleep 1 &&
                              checkpid $pid && sleep $delay &&
                              checkpid $pid ; then
                                kill -KILL $pid >/dev/null 2>&1
                                usleep 100000
                           fi
                        fi
                        checkpid $pid
                        RC=$?
                        [ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
                        RC=$((! $RC))
                # use specified level only
                else
                        if checkpid $pid; then
                                kill $killlevel $pid >/dev/null 2>&1
                                RC=$?
                                [ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
                        elif [ -n "${LSB:-}" ]; then
                                RC=7 # Program is not running
                        fi
                fi
        else
                if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
                        RC=7 # Program is not running
                else
                        failure $"$base shutdown"
                        RC=0
                fi
        fi

        # Remove pid file if any.
        if [ -z "$killlevel" ]; then
            rm -f "${pid_file:-/var/run/$base.pid}"
        fi
        return $RC
}

# Check if $pid (could be plural) are running
checkpid() {
        local i

        for i in $* ; do
                [ -d "/proc/$i" ] && return 0
        done
        return 1
}

# Log that something succeeded
success() {
  #if [ -z "${IN_INITLOG:-}" ]; then
  #   initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
  #fi
  [ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
  return 0
}

echo_success() {
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $"  OK  "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -ne "\r"
  return 0
}

$在echo线中的相关性是什么?

echo -n $"Shutting down Launcher: "

如果我添加调试行:

echo "RETVAL=$RETVAL"
[ $RETVAL -eq 0 ] && rm -f $PIDFILE_L

我明白了:

Shutting down Dispatcher:                             [FAILED]
RETVAL=0 down Launcher:                               [  OK  ]
Shutting down Job Selector:                           [FAILED]

那里有任何bash脚本专家吗?

1 个答案:

答案 0 :(得分:0)

在下一行之前,我所需要的只是一个echo语句。

echo
[ $RETVAL -eq 0 ] && rm -f $PIDFILE_L

这与我关于echo -n $"Shutting down Launcher"的行为的问题有关。

回显-n开关会抑制换行符\n。引号前面的$输出回车\r,将光标移回到行的开头。

如果没有echo语句,作业选择器关闭行将覆盖Launcher关闭行。