Friday, April 8, 2011

Install Sphinx as Service on Redhat/CentOS

Thanks Steve Kamerman

Create this searchd script under /etc/init.d, & alter the SUDO_USER & BASEPATH to fit your environment.

#!/bin/bash## Init file for searchd## chkconfig: 2345 55 25## description: searchd ## USE "chkconfig --add searchd" to configure Sphinx searchd service## by Steve Kamerman April 14, 2010# public domainSUDO_USER=sphinxBASE_PATH=/usr/local/sphinxPID_FILE=$BASE_PATH/var/log/searchd.pidCONFIG_FILE=$BASE_PATH/etc/sphinx.confEXEC_PATH=$BASE_PATH/binLOG_PATH=$BASE_PATH/var/logDATA_PATH=$BASE_PATH/var/dataRETVAL=0prog="searchd"do_config() {	chown -R $SUDO_USER $EXEC_PATH	chown -R $SUDO_USER $CONFIG_FILE	chown -R $SUDO_USER $DATA_PATH	chown -R $SUDO_USER $LOG_PATH	chmod 600 $CONFIG_FILE	chmod u+rwx $EXEC_PATH/*	chmod -R u+rw,go-rwx $DATA_PATH	chmod -R u+rw,go-rwx $LOG_PATH}do_start() {	echo "Starting $prog"	sudo -u $SUDO_USER $EXEC_PATH/$prog --config $CONFIG_FILE	RETVAL=$?	echo	return $RETVAL}do_stop() {	echo "Stopping $prog"	if [ -e $PID_FILE ] ; then		sudo -u $SUDO_USER $EXEC_PATH/$prog --stop		sleep 2		if [ -e $PID_FILE ] ; then			echo "WARNING: searchd may still be alive: $PID_FILE"		fi	fi	RETVAL=$?	echo	return $RETVAL}do_status() {	RETVAL=$?	if [ -e $PID_FILE ] ; then 		sudo -u $SUDO_USER $EXEC_PATH/$prog --status		echo "---"		echo "$prog is running (`cat $PID_FILE`)"	else		echo "$prog is not running"	fi	return $RETVAL}do_reindex() {	echo "Reindexing all $prog indices"	if [ -e $PID_FILE ] ; then		sudo -u $SUDO_USER $EXEC_PATH/indexer --all --rotate	else		sudo -u $SUDO_USER $EXEC_PATH/indexer --all	fi	echo "done."	echo	RETVAL=$?	return $RETVAL}case $* inconfig)	do_config	;;start)	do_start	;;stop)	do_stop	;;status)	do_status	;;reindex)	do_reindex	;;*)	echo "usage: $0 {start|stop|config|status|reindex}" >&2	exit 1	;;esacexit $RETVAL

Then alter the permission to make it executable.

chown root:root /etc/init.d/searchdchmod 755 /etc/init.d/searchd

Finalise, make it automatically start on system boot

chkconfig --level 345 searchd on

Now you can use

service searchd start/stop/status

to control the sphinx service