Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i start Apache Cassandra as a service?

Tags:

cassandra

I have Apache Cassandra 2.1.0 on Amazon instance with Ubuntu 14.Is it possible to start Apache Cassandra as a service?

like image 747
sparrow Avatar asked Dec 18 '14 10:12

sparrow


People also ask

How do I know if my Cassandra service is running?

Check the status of the Cassandra nodes in your cluster - Go to the /<Install_Dir>/apache-cassandra/bin/ directory and type the ./nodetool status command. If the status for all the nodes shows as UN , then the nodes are up and running. If the status for any node shows as DN , then that particular node is down.


3 Answers

I don't have reputation to comment, but please do NOT use these start scripts suggested by others right out of the box!

The following issues are present in them:

  • The server is started as root user, very bad!
  • The error checking is flawed, the test for $? after a sleep is sure to lead 0
  • A few more issues that stem from the use of root as creator of the PID file

A corrected script for Debian below:

#!/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
like image 96
calloc_org Avatar answered Sep 28 '22 03:09

calloc_org


Yes you can, If you are using debian package then automatically it will register as a service. But you are using tar, so you need to follow the steps given in the below link.

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

There is no matter it is debian or cent family when you are using tarball.

like image 33
Jaya Ananthram Avatar answered Sep 28 '22 02:09

Jaya Ananthram


You need to create the startup script cassandra and put that script into the /etc/init.d/.

You can refer to the link

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

after that you can use the following commands to start and stop the cassandra as a serivce.

  • Start : - $service cassandra start
  • Stop : - $service cassandra stop
like image 41
Rahul Wagh Avatar answered Sep 28 '22 03:09

Rahul Wagh