Starting and Stopping Cassandra

On the initial start up, each node must be started one at a time, starting with your seed nodes.

Stopping and Starting is different based upon if cassandra was installed with a Package Manager or Tarball

Starting Cassandra - Packaged Installation

Start Cassandra

sudo service cassandra start 

If Cassandra fails to start:

Reloading systemd:                                         [  OK  ]
Starting cassandra (via systemctl):  Job for cassandra.service failed because a configured resource limit was exceeded. 
See "systemctl status cassandra.service" and "journalctl -xe" for details.
                                                               [FAILED]

Cassandra Service

The Cassandra service is not enabled on newer Linux systems, which use systemd. To verify use:

sudo systemctl is-enabled cassandra.service

Enable the service:

sudo systemctl enable cassandra.service

Start Cassandra

sudo service cassandra start

Starting Cassandra - Binary Tarball

To start Cassandra in the background:

If Cassandra fails to start:

cd install_directory
bin/cassandra #Starts Cassandra

To start Cassandra in the foreground:

$ cd install_directory 
$ bin/cassandra -f #Starts Cassandra

If you are on a Linux distro that supports systemd, you can create the required service file for Cassandra, to ensure auto-restart, or start upon reboot is working.

  1. Create cassandra.service and open for editing
vi /etc/systemd/system/cassandra.service
  1. Add the following to cassandra.service
[Unit]
Description=Apache
Cassandra After=network.target

 [Service]
PIDFile=/opt/cassandra/cassandra.pid
User=cassandra
Group=cassandra
ExecStart=_/opt/cassandra/apache-cassandra-3.11.10/bin/cassandra _ -f -p     /opt/cassandra.pid
Restart=always
[Install]
WantedBy=multi-user.target
  1. Reload the daemon
systemctl daemon-reload
  1. Next, start the Cassandra service and enable it to start after system reboot with the following command:
systemctl start Cassandra
systemctl enable Cassandra
  1. You can also check the status of the Cassandra service with the following command:
systemctl status Cassandra
  1. To stop the service:
systemctl stop cassandra
  1. Create init.d and open for editing
vi  /etc/init.d/cassandra
  1. Add the following to init.d
#!/bin/sh
#
# chkconfig: - 80 45

# description: Starts and stops Cassandra

# update daemon path to point to the cassandra executable

CASSANDRA_HOME="/opt/cassandra/apache-cassandra-3.11.10"
CASSANDRA_PROG="/opt/cassandra/apache-cassandra-3.11.10/bin/cassandra"
CASSANDRA="Cassandra"
CASSANDRA_OWNR="ksr_cassandra"

log_file="/var/log/cassandra/cassandra.log"
pid_file="$CASSANDRA_HOME/ksr_cassandra.pid"


function status {
    RUNNING_PID=0
    if [ -f $pid_file ]; then
        TMP_PID=`cat $pid_file`
        TMP_PID_CHECK=`ps -p $TMP_PID -o pid=`
        if [ "$TMP_PID_CHECK" != "" ]; then
            RUNNING_PID=$TMP_PID
            return 0  # running
        else
            return 1  # stopped, but pid file exists
        fi
    fi
    return 3 # stopped
}

start() {
    if [ -f $pid_file ]; then
        echo "Cassandra is already running"
        exit 0
    fi 
    echo -n "Starting Cassandra... "
    runuser -u $CASSANDRA_OWNR -- $CASSANDRA_PROG -p $pid_file > $log_file 2>&1
    retval=$?
    if [ $retval -eq 0 ]; then
             echo "OK"
        else
            echo "Start Failed"
            exit 1
    fi
    
    return $retval
}

stop() {
    if [ ! -f $pid_file ]; then
        echo "Cassandra is already stopped"
        exit 0
    fi 
    echo -n "Stopping Cassandra... "
    runuser -u $CASSANDRA_OWNR -- kill $(cat $pid_file)
    retval=$?
    if [ $retval -eq 0 ]; then   
         echo "OK"
    else
        echo "Cassandra Stop Failed"
        exit 1
    fi
    return $retval
}

case "$1" in

  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        stop
        start
        ;;
   status)
       status

      retval=$?
       if [ $retval -eq 0 ]; then
            echo "${CASSANDRA} (pid $RUNNING_PID) is running..."
       elif [ $retval -eq 1 ]; then
            echo "${CASSANDRA} is dead but pidfile ($pid_file) exists!"
       else
            echo "${CASSANDRA} is stopped."
       fi
      exit $RET
        ;;

  *)

        echo $"Usage: $0 {start|stop|restart|status}"

        exit 1

esac

exit $?
  1. Make the file executable:
sudo chmod +x /etc/init.d/cassandra
  1. Add the new service to the list:
sudo chkconfig --add cassandra
  1. Now you can manage the service from the command line:
sudo /etc/init.d/cassandra start
sudo /etc/init.d/cassandra stop
sudo /etc/init.d/cassandra restart

Checking the Progress Of Cassandra

To monitor the progress of the startup:

tail -f logs/system.log

Cassandra is ready when it shows an entry like this in the system.log:

INFO  [main] 2019-12-31 03:03:37,526 Server.java:156 - Starting listening for CQL clients on /x.x.x.x:9042 (unencrypted)...`

To check the status of Cassandra:

bin/nodetool status

The status column in the output should report UN which stands for Up/Normal.

Stopping Cassandra - Packaged Installation

You must have root or sudo permissions to stop the Cassandra service

Package Installations

sudo service cassandra stop

Find the Cassandra Java process ID (PID). If Cassandra is still running, kill the process using its PID number:

    ps auwx | grep cassandra
    sudo kill pid #Stop Cassandra

Stopping Cassandra - Binary Tarball

Find the Cassandra Java process ID (PID), and then kill the process using its PID number:

ps auwx | grep cassandra
sudo kill pid #Stop Cassandra

If you have an init.d file created for cassandra. (described above)

sudu /etc/init.d/cassandra stop