Cassandra启动脚本失败并带有status选项

时间:2016-10-14 14:27:56

标签: cassandra

Cassandra数据库运行正常,但当我执行/etc/init.d/cassandra status时,它会显示cassandra is stopped。在脚本中,我将pid位置硬编码为/var/run/cassandra/cassandra.pid,在调试模式下执行脚本后,注意到脚本使用了不正确的pid /var/run/cassandra.pid文件位置。这是下面的输出。知道脚本在哪里获得这个pid位置信息。

调试输出。

+ . /etc/sysconfig/network
++ NETWORKING_IPV6=no
++ HOSTNAME=rvagcas01
++ NETWORKING=yes
++ NOZEROCONF=yes
+ prog=Cassandra
+ pidfile=/var/run/cassandra/cassandra.pid
+ progbin=/db/cassandra/bin
+ lock=/db/cassandra/cassandra.lock
+ logfile=/db/cassandra/logs/service.log
+ user=cassandra
+ case "$1" in
+ status cassandra
+ local base pid lock_file= pid_file= binary=
+ '[' 1 = 0 ']'
+ '[' cassandra = -p ']'
+ '[' cassandra = -l ']'
+ '[' cassandra = -b ']'
+ base=cassandra
+ __pids_var_run cassandra '' ''
+ local base=cassandra
+ local pid_file=/var/run/cassandra.pid
++ /usr/bin/dirname /var/run/cassandra.pid
+ local pid_dir=/var/run
+ local binary=
+ '[' -d /var/run -a '!' -r /var/run ']'
+ pid=
+ '[' -f /var/run/cassandra.pid ']'
+ return 3
+ RC=3
+ '[' -z '' -a -z '' ']'
++ __pids_pidof cassandra
++ pidof -c -m -o 25634 -o 23662 -o %PPID -x cassandra
++ pidof -c -m -o 25634 -o 23662 -o %PPID -x cassandra
+ pid=
+ '[' -n '' ']'
+ case "$RC" in
+ '[' -z '' ']'
+ lock_file=cassandra
+ '[' -f /var/lock/subsys/cassandra ']'
+ echo 'cassandra is stopped'
cassandra is stopped
+ return 3
+ exit 3

这是启动脚本。

    # cat /etc/init.d/cassandra
    #!/bin/bash
    # Cassandra database
    ### BEGIN INIT INFO
    # Provides:             cassandra
    # Required-Start:       $remote_fs $all
    # Required-Stop:
    # Default-Start:        2 3 4 5
    # Default-Stop:         0 1 6
    # Short-Description:    Apache Cassandra database server
    # Description:          Apache Cassandra database server
    ### END INIT INFO#
    # Author:
    #
    #
    # Description:  Cassandra Server.
    # Processname: cassandra
    # Config: /app/cassandra/conf/cassandra.yaml
    set -x

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

    # Source networking configuration.
    . /etc/sysconfig/network

    prog="Cassandra"
    pidfile="/var/run/cassandra/cassandra.pid"
    progbin="/app/cassandra/bin"
    lock="/app/cassandra/cassandra.lock"
    logfile="/app/cassandra/logs/service.log"
    user=cassandra

    WriteLog()
    {
        echo "`date`: $@" >> $logfile
    }

    LogInfo()
    {
        echo "$@"
        WriteLog "INFO: $@"
    }

    LogWarning()
    {
        echo "$@"
        WriteLog "WARNING: $@"
    }

    start()
    {
        if [ -f $pidfile ] && checkpid `cat $pidfile`; then
            action "$prog is already running." /bin/false
            exit 0
        fi

        WriteLog "Starting $prog"

        daemon --user $user "$progbin/cassandra" -p $pidfile >> $logfile 2>&1
        usleep 500000
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
            touch "$lock"
            action "Starting $prog" /bin/true
        else
            action "Starting $prog" /bin/false
        fi

        WriteLog "Started $prog"

        return $RETVAL
    }

    stop()
    {
        $progbin/nodetool -h localhost disablethrift
        $progbin/nodetool -h localhost disablegossip
        $progbin/nodetool -h localhost drain
        WriteLog "Stopping $prog"
        CASSIEPID=`cat "$pidfile"  2>/dev/null `
        if [ -n "$CASSIEPID" ]; then
            /bin/kill "$CASSIEPID" >/dev/null 2>&1
            ret=$?
            if [ $ret -eq 0 ]; then
                STOPTIMEOUT=60
                while [ $STOPTIMEOUT -gt 0 ]; do
                    /bin/kill -0 "$CASSIEPID" >/dev/null 2>&1 || break
                    sleep 1
                    let STOPTIMEOUT=${STOPTIMEOUT}-1
                done
                if [ $STOPTIMEOUT -eq 0 ]; then
                    echo "Timeout error occurred trying to stop $prog Daemon"
                    ret=1
                    action $"Stopping $prog: " /bin/false
                    LogInfo "Timeout error occurred trying to stop $prog Daemon pid($CASSIEPID)"
                else
                    rm -f "$lock"
                    action $"Stopping $prog: " /bin/true
                    WriteLog "INFO: $prog Daemon Stopped pid($CASSIEPID)"
                fi
            else
                action $"Stopping $prog: " /bin/false
                WriteLog "WARNING: $prog Daemon Stop Failed pid($CASSIEPID)"
            fi
        else
            ret=1
            action $"Stopping $prog: " /bin/false
        fi
        return $ret
    }

    restart()
    {
        LogInfo "Restart Initiated"
        stop
        start
    }

    ring()
    {
        $progbin/nodetool -h localhost ring
    }

    info()
    {
        $progbin/nodetool -h localhost info
    }

    netstats()
    {
        $progbin/nodetool -h localhost netstats
    }

    repair()
    {
        LogInfo "Starting Repair"
        $progbin/nodetool -h localhost repair
        LogInfo "Completed Repair"
    }

    cleanup()
    {
        LogInfo "Starting Cleanup"
        $progbin/nodetool -h localhost cleanup
        LogInfo "Completed Cleanup"
    }

    version()
    {
        $progbin/nodetool -h localhost version
    }

    # See how we were called.
    case "$1" in
      start)
        start
        ;;
      stop)
        stop
        ;;
      status)
        status cassandra
        ;;
      restart)
        restart
        ;;
      ring)
        ring
        ;;
      info)
        info
        ;;
      netstats)
        netstats
        ;;
      repair)
        repair
        ;;
      cleanup)
        cleanup
        ;;
      version)
        version
        ;;
      *)
        echo $"Usage: $0 {start|stop|status|restart|ring|info|netstats|repair|cleanup|version}"
        exit 1
    esac

    exit $?

#

1 个答案:

答案 0 :(得分:0)

我没有您用于开始/停止/状态的脚本。

我猜你的调试输出中的原因是__proc_pids,该脚本硬编码了该位置。

/etc/init.d/functions

# __proc_pids {program} [pidfile]
# Set $pid to pids from /var/run* for {program}.  $pid should be declared
# local in the caller.
# Returns LSB exit code for the 'status' action.
__pids_var_run() {
    local base=${1##*/}
    local pid_file=${2:-/var/run/$base.pid}

你可以参考这个例子,我浏览一下,似乎可以满足你的要求。

https://gist.github.com/sgomezvillamor/5458309