Thursday, July 23, 2015

Start Apache Cassandra as a service on CentOS

Start Apache Cassandra as a service on CentOS
If you are working on CentOS and you want to start/stop Cassandra as as a service than please have look at the following script.
  • Create a file cassandra and put the following line of code in it as it is. Save the file without any extension

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

  • Please update the following path as per your unix system
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


  • Copy the file to /etc/init.d/
  • If you have created the this file on Windows and trying to move on the CentOS than please use $dos2unix cassandra command after copying.
  • Now its good to go you can now user following command for cassandra
  • Start : - $service cassandra start
  • Stop : - $service cassandra stop

6 comments:

  1. if there is an exception then the service stops running and i have to manually run the command to start it again. Is there any way to restart it automatically? Thanks in advance.

    ReplyDelete
  2. Hi Pooja,

    Thanks for your comment.

    With respect to your concern if there is any exception and service stops running than I think you need to restart it manually. I am not an expert on Linux scripting but as per my experience if exception occurs than it explicitly shutdown the Cassandra service than only way left is to start it explicitly by providing the start command.

    Let me know if you found any alternative to start it automatically.

    Regards,
    Rahul wagh

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Will this script work for CentOS 7.x also?

    ReplyDelete
  5. After reading this blog i very strong in this topics and this blog really helpful to all... explanation Big Data Hadoop Online Course Hyderabad

    ReplyDelete
  6. I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
    Believe me I did wrote an post about tutorials for beginners with reference of your blog. 




    Selenium training in bangalore
    Selenium training in Chennai
    Selenium training in Bangalore
    Selenium training in Pune
    Selenium Online training

    ReplyDelete