Sphinx search init script for CentOS 5

I was looking for a sophisticated init script for sphinx and came across a great script here : http://www.sphinxsearch.com/wiki/doku.php?id=for_centos_5. It did not have an option for specifying a custom path to the configuration file though. It took me a few minutes to add that option and I hope it helps someone.

I also wanted only the root user to be able to run the init script, so added a few lines of code to do that :)

#!/bin/sh
# This script was taken from http://www.sphinxsearch.com/wiki/doku.php?id=for_centos_5 and modified by Pritesh Shah
# Feel free to use/modify it as you like :)
# sphinx: Startup script for Sphinx search
#
# chkconfig: 345 86 14
# description:  This is a daemon for high performance full text \
#               search of MySQL and PostgreSQL databases. \
#               See http://www.sphinxsearch.com/ for more info.
#
# processname: searchd
# pidfile: $sphinxlocation/var/log/searchd.pid

if [ $UID -ne 0 ]; then
echo 'You need to be root!'
exit
fi
# Source function library.
. /etc/rc.d/init.d/functions

# EDIT THESE PARAMETERS BELOW #
servicename=sphinx
username=root
sphinxlocation=/usr/local/sphinx
pidfile=$sphinxlocation/var/log/searchd.pid
configfile=$sphinxlocation/etc/sphinx.conf
processname=$sphinxlocation/bin/searchd

# DO NOT EDIT ANYTHING BELOW THIS LINE -- YOU SHOULD NOT NEED TO #

RETVAL=0

PATH=$PATH:$sphinxlocation/bin

start() {
echo -n $"Starting Sphinx daemon: "
daemon --user=$username --check $servicename $processname --config $configfile
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$servicename
}

stop() {
echo -n $"Stopping Sphinx daemon: "

killproc -p $pidfile $servicename -TERM
RETVAL=$?
echo
if [ $RETVAL -eq 0 ]; then
rm -f /var/lock/subsys/$servicename
rm -f $pidfile
fi
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $processname
RETVAL=$?
;;
restart)
stop
sleep 3
start
;;
condrestart)
if [ -f /var/lock/subsys/$servicename ]; then
stop
sleep 3
start
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart}"
;;
esac
exit $RETVAL

Save this script as sphinx in the /etc/init.d folder.
Then, make root as the owner and set executable permissions for the script.

sudo chown root:root /etc/init.d/sphinx
sudo chmod 744 /etc/init.d/sphinx

Add sphinx to system startup

sudo /sbin/chkconfig --add sphinx

Done!

Leave a Reply