Run Cassandra as CentOS Service

In this post i’ll show you how to make Cassandra as CentOS service, so that you can run cassandra with commands like start, stop, restart or status.

The other advantage is you can make cassandra to run in startup. Lets start now.

First make a file called cassandra using below command

nano cassandra

This command opens up nano editor, now you are ready to make some scripts.

First make some constants

CASS_HOME=/opt/apache-cassandra-2.1.8
CASS_BIN=$CASS_HOME/bin/cassandra
CASS_LOG=$CASS_HOME/log/cassandra.out
CASS_PID=$CASS_HOME/pid/cassandra.pid
prog=Cassandra

As you see first define CASS_HOME as cassandra path you have extracted the cassandra, secondly you must define the bin of the cassandra using cassandra home.

Next two is where you want to log the logs which is displayed on console and path to save the pid.

Now lets write start script.

start() {
if [ -f $CASS_PID ] && checkpid `cat $CASS_PID`; then
echo “Cassandra is already running.”
exit 0
fi
echo -n $”Starting $prog: ”
$CASS_BIN -p $CASS_PID >> $CASS_LOG 2>&1
echo success
return $RETVAL
}

This Scripts checks if PID exist then it displays cassandra is already running else it starts the cassandra and saves pid in a file

Lets see stop scripts

stop() {
# check if the process is already stopped by seeing if the pid file exists.
if [ ! -f $CASS_PID ]; then
echo “Cassandra is already stopped.”
exit 0
fi
echo -n $”Stopping $prog: ”
if kill `cat $CASS_PID`; then
RETVAL=0
echo success
else
RETVAL=1
echo failure
fi
echo
[ $RETVAL = 0 ]
}

This scripts check if cassandra is already stopped else it kills the pid which is stored in pid file

Lets see Status Script

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

This checks if pid exist then it shows its running else stopped.

The full Script is

#!/bin/bash
# init script for Cassandra.
# chkconfig: 2345 90 10
# description: Cassandra
CASS_HOME=/opt/apache-cassandra-2.1.8
CASS_BIN=$CASS_HOME/bin/cassandra
CASS_LOG=$CASS_HOME/log/cassandra.out
CASS_PID=$CASS_HOME/pid/cassandra.pid
prog=Cassandra

if [ ! -f $CASS_BIN ]; then
echo “File not found: $CASS_BIN”
exit 1
fi

RETVAL=0
start() {
if [ -f $CASS_PID ] && checkpid `cat $CASS_PID`; then
echo “Cassandra is already running.”
exit 0
fi
echo -n $”Starting $prog: ”
$CASS_BIN -p $CASS_PID >> $CASS_LOG 2>&1
echo success
return $RETVAL
}

stop() {
# check if the process is already stopped by seeing if the pid file exists.
if [ ! -f $CASS_PID ]; then
echo “Cassandra is already stopped.”
exit 0
fi
echo -n $”Stopping $prog: ”
if kill `cat $CASS_PID`; then
RETVAL=0
echo success
else
RETVAL=1
echo failure
fi
echo
[ $RETVAL = 0 ]
}

status_fn() {
if [ -f $CASS_PID ] && checkpid `cat $CASS_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
sleep 5
start
;;
*)
echo $”Usage: $prog {start|stop|restart|status}”
RETVAL=3
esac

exit $RETVAL


Now you paste this cassandra file under /etc/init.d

Add cassandra service to chkconfig, command is

chkconfig –add cassandra

Now you can start or stop or check status using below command

service cassandra start

service cassandra stop

service cassandra status

If you want to start cassandra on startup the use below command

chkconfig cassandra on

To remove from startup use below command

chkconfig cassandra off

Leave a Reply

Your email address will not be published. Required fields are marked *