Skip to content

Instantly share code, notes, and snippets.

@ovr
Created August 11, 2013 05:30
Show Gist options
  • Save ovr/6203521 to your computer and use it in GitHub Desktop.
Save ovr/6203521 to your computer and use it in GitHub Desktop.
#! /bin/bash
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian
# by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
# Modified for sphinx by Radu Spineanu <radu@debian.org>
#
#
### BEGIN INIT INFO
# Provides: sphinxsearch
# Required-Start: $local_fs $remote_fs $syslog $network $time
# Required-Stop: $local_fs $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Fast standalone full-text SQL search engine
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/searchd
DESC=sphinxsearch
test -x $DAEMON || exit 0
LOGDIR=/var/log/sphinxsearch
RUNDIR=/var/run/sphinxsearch
LIBDIR=/var/lib/sphinxsearch
# Include sphinxsearch defaults if available
if [ -f /etc/default/sphinxsearch ] ; then
. /etc/default/sphinxsearch
fi
if [ "$START" != "yes" ]; then
echo "To enable $NAME, edit /etc/default/sphinxsearch and set START=yes"
exit 0
fi
set -e
running_pid()
{
# Check if a given process pid's cmdline matches a given name
pid=$1
name=$2
[ -z "$pid" ] && return 1
[ ! -d /proc/$pid ] && return 1
cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
# Is this the expected child?
[ "$cmd" != "$name" ] && return 1
return 0
}
running()
{
# Check if the process is running looking at /proc
# (works for all users)
# No pidfile, probably no daemon present
[ ! -f "$PIDFILE" ] && return 1
# Obtain the pid and check it against the binary name
pid=`cat $PIDFILE`
running_pid $pid $DAEMON || return 1
return 0
}
FILES=(/etc/sphinxsearch/sphinx_*.conf);
# check for alternative config schema
if [ -r "${FILES[0]}" ]; then
CONFIGS=();
for FILE in "${FILES[@]}";
do
# remove prefix and sufix
NAME=$(basename "${FILE}" .conf);
# check optional second param
if [ $# -ne 2 ];
then
# add to config array
CONFIGS+=($NAME);
elif [ "sphinx_$2" == "$NAME" ];
then
# use only one redis
CONFIGS=($NAME);
break;
fi;
done;
if [ ${#CONFIGS[@]} == 0 ];
then
echo "Config not exist for: $2" >&2;
exit 1;
fi;
else
CONFIGS=(sphinx);
fi;
CONFIG_NUM=${#CONFIGS[@]};
for ((i=0; i < $CONFIG_NUM; i++)); do
NAME=${CONFIGS[${i}]};
PIDFILE=$LIBDIR/${NAME}.pid
DAEMON_ARGS="-c /etc/sphinxsearch/${NAME}.conf"
case "$1" in
start)
echo -n "Starting $DESC: "
# Check if we have the configuration file
if [ ! -f /etc/sphinxsearch/${NAME}.conf ]; then
echo "Please create an ${NAME}.conf configuration file."
echo "Templates are in the /etc/sphinxsearch/ directory."
exit 0
fi
start-stop-daemon --start --chuid ${RUNUSER} --quiet --pidfile $PIDFILE --exec ${DAEMON} -- $DAEMON_ARGS
if running ; then
echo "$NAME."
else
echo " ERROR."
fi
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE \
--exec $DAEMON
echo "$NAME."
;;
force-stop)
echo -n "Forcefully stopping $DESC: "
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE \
--exec $DAEMON --retry 5
if ! running ; then
echo "$NAME."
else
echo " ERROR."
fi
;;
restart)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE \
--exec $DAEMON --retry 5
[ -n "$DODTIME" ] && sleep $DODTIME
start-stop-daemon --start --chuid ${RUNUSER} --quiet --pidfile $PIDFILE --exec ${DAEMON} -- $DAEMON_ARGS
echo "$NAME."
;;
status)
echo -n "$NAME is "
if running ; then
echo "running"
else
echo "not running."
fi
;;
*)
N=/etc/init.d/$DESC
# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
exit 1
;;
esac
done;
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment