Skip to content

Instantly share code, notes, and snippets.

@sgomezvillamor
Last active May 21, 2020 08:57
Show Gist options
  • Save sgomezvillamor/5458309 to your computer and use it in GitHub Desktop.
Save sgomezvillamor/5458309 to your computer and use it in GitHub Desktop.
/etc/init.d/cassandra for CentOS
#!/bin/bash
# init script for Cassandra.
# chkconfig: 2345 90 10
# description: Cassandra
# script slightly modified from
# http://blog.milford.io/2010/06/installing-apache-cassandra-on-centos/
. /etc/rc.d/init.d/functions
CASS_HOME=/cassandra/apache-cassandra-1.2.4
CASS_BIN=$CASS_HOME/bin/cassandra
CASS_LOG=$CASS_HOME/../log/system.log
CASS_USER="root"
CASS_PID=/var/run/cassandra.pid
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: "
daemon --user $CASS_USER $CASS_BIN -p $CASS_PID >> $CASS_LOG 2>&1
usleep 500000
RETVAL=$?
if [ "$RETVAL" = "0" ]; then
echo_success
else
echo_failure
fi
echo
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
usleep 500000
start
;;
*)
echo $"Usage: $prog {start|stop|restart|status}"
RETVAL=3
esac
exit $RETVAL
@kenwdelong
Copy link

Does this delete the pidfile when the service is stopped?

@gembin
Copy link

gembin commented Jan 22, 2016

Any script for RHEL/CentOS 7 ?

@bschopman
Copy link

In start() on line 31 the statement RETVAL=$? doesn't get the return value of daemon --user ..., but that of usleep 500000, because that's the last executed command. This will always be 0, because the sleep command barely ever fails. Move the statement on line 31 up one line to fix this bug.

Copy link

ghost commented Apr 26, 2016

Usually init.d scripts should have the descriptor.
You could put this after you initial comment block:

### BEGIN INIT INFO
# Provides: cassandra
# Required-Start: $local_fs $network $syslog
# Required-Stop: $local_fs $syslog
# Should-Start: $syslog
# Should-Stop: $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start Cassandra Service
# Description:       Cassandra Database Server
### END INIT INFO 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment