CentOS Cassandra init (start / stop) script

For CentOS I created an init script to start it at boot time and shut it down for rebooting or power off. If you want to use this init script for your own machine please look through the script and change the necessary parameters, e.g. CASSANDRA_HOME.

Please also note that in the stop function it decommissions the node from the cluster. This is probably not what you want in production environments.

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

If all looks alright, name the script cassandra and place it in /etc/init.d/
After that run the following commands:

mkdir /opt/apache-cassandra-0.7.4/log
chmod +x /etc/init.d/cassandra
chkconfig --add cassandra
service cassandra start

4 thoughts on “CentOS Cassandra init (start / stop) script

  1. Permalink  ⋅ Reply

    Todd Palmieri

    September 11, 2011 at 5:59pm

    Thanks for the help on this! Cassandra is NOT an easy install to get running.

    When i try to run “chkconfig –add cassandra” I keep getting “service cassandra does not support chkconfig”

    I’m running Fedora 8 on EC2. I copied the script exactly, and updated the environment variables to our server.

    Can you help?

  2. Permalink  ⋅ Reply

    asd

    November 29, 2011 at 7:25pm

    thanks man, this is nice

  3. Permalink  ⋅ Reply

    Nicolas Maupu

    June 18, 2012 at 12:23pm

    Hi,

    I patched your init script to be able to alter JVM_OPTS or other options passed to cassandra without modifying either your script or cassandra core files.
    Just add a ${CASSANDRA_HOME}/bin/setenv.sh file and specify your opts there.

    Here is the patch file :
    # cat /tmp/cassandra.patch
    *** cassandra.orig 2012-06-18 12:07:29.475126749 +0200
    — cassandra 2012-06-18 12:05:27.664129224 +0200
    ***************
    *** 11,16 ****
    — 11,19 —-
    CASSANDRA_PID=/var/run/cassandra.pid
    CASSANDRA_LOCK=/var/lock/subsys/cassandra
    PROGRAM=”cassandra”
    + USER=”cassandra”
    +
    + . ${CASSANDRA_HOME}/bin/setenv.sh

    if [ ! -f $CASSANDRA_BIN ]; then
    echo “File not found: $CASSANDRA_BIN”
    ***************
    *** 25,31 ****
    exit 0
    fi
    echo -n $”Starting $PROGRAM: ”
    ! daemon –user cassandra $CASSANDRA_BIN -p $CASSANDRA_PID >> $CASSANDRA_LOG 2>&1
    usleep 500000
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
    — 28,34 —-
    exit 0
    fi
    echo -n $”Starting $PROGRAM: ”
    ! daemon –user ${USER} $CASSANDRA_BIN -p $CASSANDRA_PID >> $CASSANDRA_LOG 2>&1
    usleep 500000
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
    ***************
    *** 59,65 ****

    status_fn() {
    if [ -f $CASSANDRA_PID ] && checkpid `cat $CASSANDRA_PID`; then
    ! echo “Cassandra is running.”
    exit 0
    else
    echo “Cassandra is stopped.”
    — 62,68 —-

    status_fn() {
    if [ -f $CASSANDRA_PID ] && checkpid `cat $CASSANDRA_PID`; then
    ! echo “Cassandra is running – PID=`cat $CASSANDRA_PID`”
    exit 0
    else
    echo “Cassandra is stopped.”

    Regards,
    Nicolas Maupu

  4. Permalink  ⋅ Reply

    Sander

    May 19, 2015 at 8:57am

    Bedankt Jan Sipke for sharing this startup script!

Leave a Reply

Your email will not be published. Name and Email fields are required.