Stopping Cassandra in Shell script

Viewed 43

What shell command would you use to stop Cassandra started locally? Is there one that stops it without the PID? If no, what command can you use to get the PID in shell script?

3 Answers

Please tailor to your individual situation, since you mentioned that C* is started locally.

However you can use something like this in your script:

#!/bin/bash
PID=$(cat /var/run/cassandra/cassandra.pid)
sudo kill -9 $PID

If you started Cassandra without designating a file for the PID, then you'll have to do something tricky, like this:

ps -ef | grep cassandra | grep "jmx\.local\.port=7199" | awk '{print $2}'

96165

Basically you're using ps to get the status of all processes, grepping for "cassandra", and then grepping for something very specific to the Cassandra process (like local JMX on port 7199). Then, if you pipe that all through awk, you can pretty easily just pull the second space-delimited "column."

There's probably a more graceful way to do this, but it'll work.

Then, as a user with privileges to the cassandra process, you can just run a kill on that PID. Or, you could encapsulate that all into one, single command:

kill $(ps -ef | grep cassandra | grep "jmx\.local\.port=7199" | awk '{print $2}')

Stopping a local Cassandra instance depends on the installation type.

If you've installed Cassandra as a YUM (RHEL/CentOS) or APT (Debian) package, stop the service with:

$ sudo service cassandra stop

If you've installed Cassandra using the binary tarball, you can simply kill it with:

$ pkill cassandra

Note that if you're running Cassandra as another user such as root, you'll need to use sudo to get elevated permissions. Cheers!

Related