Pastebin

Paste #4149: server_wrap.sh

< previous paste - next paste>

Pasted by server_wrap.sh

Download View as text

#!/bin/sh

SLEEP_INTERVAL=5

stop_and_exit()
{
    "${CMD}" stop >> /dev/null
    exit $?
}

debug()
{
    # When the env var DEBUG is set, log extra output to stderr
    if [ ! -z "${DEBUG}" ]
    then
        for line in "$@"
        do
            echo "${line}" >&2
        done
    fi
}

########
# MAIN #
########
if [ $# -lt 1 ]
then
    echo "Usage: $0 <CMD> [<STATUS CMD> [<STATUS ARGS>]]" >&2
    echo "Where:" >&2
    echo "  <CMD> is the full path to server control script." >&2
    echo "  <STATUS CMD> is the full path to server status command." >&2
    echo "  <STATUS ARGS> is additional arguments for the server status command." >&2
    exit 2 # LSB compliant status; invalid or excess argument(s)
fi

CMD="$1"
debug "Server control script: '${CMD}'"
shift
if [ ! -e "${CMD}" ]
then
    echo "${CMD} must be the full path to a command script." >&2
    exit 2 # LSB compliant status; invalid or excess argument(s)
fi
if [ ! -x "${CMD}" ]
then
    echo "${CMD} cannot be executed." >&2
    exit 4 # LSB compliant status; user had insufficient privilege
fi
CMD_NAME=`basename "${CMD}"`

STATCMD=
if [ $# -ne 0 ]
then
    STATCMD="$1"
    shift
    debug "Status cmd was specified: '${STATCMD}'"
    debug "Status cmd arguments ($#):" "--$@--"
    if [ ! -e "${STATCMD}" ]
    then
        echo "${STATCMD} must be the full path to the service status command." >&2
        exit 2 # LSB compliant status; invalid or excess argument(s)
    fi
    if [ ! -x "${STATCMD}" ]
    then
        echo "${STATCMD} cannot be executed." >&2
        exit 4 # LSB compliant status; user had insufficient privilege
    fi
fi

# Start the service
"${CMD}" start >>/dev/null
start_rc=$?
if [ ${start_rc} -ne 0 ]
then
    echo $"${CMD_NAME} failed to start." >&2
    exit ${start_rc}
fi
debug "Service ${CMD_NAME} was started..."

# Service should be started; install signal handler which will stop the service
trap stop_and_exit 1 2 3 6 15

while `exit 0`
do  # pause first to give service time to fully initialize
    sleep ${SLEEP_INTERVAL}

    if [ ! -z "${STATCMD}" ]
    then
        stat_out=`"${STATCMD}" "$@"` # NOTE: passing along additional args "$@" to status cmd
        mon_rc=$?
        debug "--Service ${CMD_NAME} status (${mon_rc})..."
        [ ${mon_rc} -ne 0 ] && break
        # SAS init scripts status functions exit 0 when server is stopped, must check the output
        [ `echo "${stat_out}" | grep -c "is stopped$"` -ne 0 ] && break
    else
        ## No monitor cmd given ##
        # Get list of pids in the process group that were spawned by start cmd
        # Since start cmd has exited the ppid will be init, i.e. ppid=1
        # NOTE: This doesn't work on HPUX; ps command doesn't support -o option
        pgpids=`ps -o pgid,pid,ppid | grep "^[[:space:]]*$$[[:space:]]" | grep "[[:space:]]1$" | awk '{print $2}'`
        pgstr=`echo ${pgpids}` # Strip off newlines
        [ -z "${pgstr}" ] && break
        debug "--Service ${CMD_NAME} group processes still running (${pgstr})..."
    fi
done

"${CMD}" stop >> /dev/null # Make certain service is stopped before exiting
exit 7 # LSB compliant status; service is not running

New Paste


Do not write anything in this field if you're a human.

Go to most recent paste.