我如何启动Apache Cassandra作为服务?

时间:2014-12-18 10:53:26

标签: cassandra

我在使用Ubuntu 14的Amazon实例上安装了Apache Cassandra 2.1.0。是否可以启动Apache Cassandra作为服务?

4 个答案:

答案 0 :(得分:8)

我没有评论的声誉,但请不要立即使用其他人建议的这些启动脚本!

其中存在以下问题:

  • 服务器以root用户身份启动,非常糟糕!
  • 错误检查有缺陷,测试是否为$?睡觉后一定要领先0
  • 使用root作为PID文件创建者的其他一些问题

以下Debian的更正脚本:

#!/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

. /lib/lsb/init-functions

CASSANDRA_HOME=/home/db/packages/apache-cassandra
CASSANDRA_BIN=$CASSANDRA_HOME/bin/cassandra
CASSANDRA_NODETOOL=$CASSANDRA_HOME/bin/nodetool
CASSANDRA_LOG=$CASSANDRA_HOME/logs/cassandra.log
CASSANDRA_PID=/var/run/cassandra.pid
CASSANDRA_LOCK=/var/lock/subsys/cassandra
PROGRAM="cassandra"
USER=db

if [ ! -f $CASSANDRA_BIN ]; then
  echo "File not found: $CASSANDRA_BIN"
  exit 1
fi

RETVAL=0

start() {
  log_action_begin_msg "Starting $PROGRAM"
  if [ -f $CASSANDRA_PID ] && checkpid `cat $CASSANDRA_PID`; then
    echo "Cassandra is already running."
    exit 0
  fi
  log_action_msg "Executing command as user $USER"
  TMPFILE=/tmp/cassandra.$RANDOM
  su --login $USER --command "$CASSANDRA_BIN -p $TMPFILE" >> $CASSANDRA_LOG 2>&1
  RETVAL=$?
  cat $TMPFILE > $CASSANDRA_PID
  rm $TMPFILE
  sleep 10
  if [ $RETVAL -eq 0 ]; then
    touch $CASSANDRA_LOCK
    log_action_end_msg $RETVAL
  else
    log_failure_msg "returned $RETVAL on startup"
  fi
  return $RETVAL
}

stop() {
  log_action_begin_msg "Stopping $PROGRAM: "
  if [ ! -f $CASSANDRA_PID ]; then
    log_action_msg "Cassandra is already stopped."
    exit 0
  fi
  $CASSANDRA_NODETOOL disablegossip
  $CASSANDRA_NODETOOL disablethrift
  $CASSANDRA_NODETOOL drain
  if kill `cat $CASSANDRA_PID`; then
    RETVAL=0
    rm -f $CASSANDRA_LOCK
    log_action_end_msg 0
  else
    RETVAL=1
    log_failure_msg "can't kill PID $CASSANDRA_PID"
  fi
  return $RETVAL
}

status_fn() {
  if [ -f $CASSANDRA_PID ] && checkpid `cat $CASSANDRA_PID`; then
    echo "Cassandra is running."
    exit 0
  else
    echo "Cassandra is stopped."
    exit 1
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status_fn
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo $"Usage: $PROGRAM {start|stop|restart|status}"
    RETVAL=3
esac

exit $RETVAL

答案 1 :(得分:2)

是的,你可以,如果您使用的是debian软件包,那么它将自动注册为服务。但是您使用的是tar,因此您需要按照以下链接中的步骤进行操作。

<强> http://jansipke.nl/centos-cassandra-init-start-stop-script/

当你使用tarball时,无论是debian还是分众家庭。

答案 2 :(得分:2)

您需要创建启动脚本cassandra并将该脚本放入 /etc/init.d /

您可以参考link

&#13;
&#13;
#!/bin/bash
# chkconfig: 2345 99 01
# description: Cassandra

. /etc/rc.d/init.d/functions

CASSANDRA_HOME=/opt/apache-cassandra-0.7.4
CASSANDRA_BIN=$CASSANDRA_HOME/bin/cassandra
CASSANDRA_NODETOOL=$CASSANDRA_HOME/bin/nodetool
CASSANDRA_LOG=$CASSANDRA_HOME/log/cassandra.log
CASSANDRA_PID=/var/run/cassandra.pid
CASSANDRA_LOCK=/var/lock/subsys/cassandra
PROGRAM="cassandra"

if [ ! -f $CASSANDRA_BIN ]; then
  echo "File not found: $CASSANDRA_BIN"
  exit 1
fi

RETVAL=0

start() {
  if [ -f $CASSANDRA_PID ] && checkpid `cat $CASSANDRA_PID`; then
    echo "Cassandra is already running."
    exit 0
  fi
  echo -n $"Starting $PROGRAM: "
  daemon $CASSANDRA_BIN -p $CASSANDRA_PID >> $CASSANDRA_LOG 2>&1
  usleep 500000
  RETVAL=$?
  if [ $RETVAL -eq 0 ]; then
    touch $CASSANDRA_LOCK
    echo_success
  else
    echo_failure
  fi
  echo
  return $RETVAL
}

stop() {
  if [ ! -f $CASSANDRA_PID ]; then
    echo "Cassandra is already stopped."
    exit 0
  fi
  echo -n $"Stopping $PROGRAM: "
  $CASSANDRA_NODETOOL -h 127.0.0.1 decommission
  if kill `cat $CASSANDRA_PID`; then
    RETVAL=0
    rm -f $CASSANDRA_LOCK
    echo_success
  else
    RETVAL=1
    echo_failure
  fi
  echo
  [ $RETVAL = 0 ]
}

status_fn() {
  if [ -f $CASSANDRA_PID ] && checkpid `cat $CASSANDRA_PID`; then
    echo "Cassandra is running."
    exit 0
  else
    echo "Cassandra is stopped."
    exit 1
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    status_fn
    ;;
  restart)
    stop
    start
    ;;
  *)
    echo $"Usage: $PROGRAM {start|stop|restart|status}"
    RETVAL=3
esac

exit $RETVAL
&#13;
&#13;
&#13;

之后,您可以使用以下命令启动和停止cassandra作为服务。

  • 开始: - $ service cassandra start
  • 停止: - $ service cassandra stop

答案 3 :(得分:1)

在Linux系统上如果cassandra作为包安装,那么启动脚本应该出现在/etc/init.d

请参阅此link

$ sudo service cassandra start