How to Configure Sphinx Search Daemon for Automatic Startup on Ubuntu Server Reboot


2 views

When deploying Sphinx Search on Ubuntu servers, ensuring continuous operation after system reboots is crucial for production environments. Ubuntu 9.04 used Upstart as its init system, which differs from modern systemd implementations but remains relevant for legacy systems.

For Ubuntu 9.04, we'll create a custom Upstart script in /etc/init/sphinxsearch.conf:

description "Sphinx Search Daemon"
author "Your Name"

start on runlevel [2345]
stop on runlevel [016]

respawn
respawn limit 10 5

exec /usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf

If you prefer SysV init style scripts, create /etc/init.d/sphinxsearch:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          sphinxsearch
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Sphinx search daemon
### END INIT INFO

SPHINX_BIN=/usr/local/sphinx/bin/searchd
SPHINX_CONFIG=/usr/local/sphinx/etc/sphinx.conf

case "$1" in
    start)
        $SPHINX_BIN --config $SPHINX_CONFIG
        ;;
    stop)
        $SPHINX_BIN --config $SPHINX_CONFIG --stop
        ;;
    restart|force-reload)
        $SPHINX_BIN --config $SPHINX_CONFIG --stopwait
        $SPHINX_BIN --config $SPHINX_CONFIG
        ;;
    *)
        echo "Usage: /etc/init.d/sphinxsearch {start|stop|restart}"
        exit 1
        ;;
esac

exit 0

Then make it executable and register the service:

chmod +x /etc/init.d/sphinxsearch
update-rc.d sphinxsearch defaults

After creating your preferred configuration, test the automatic startup:

# For Upstart:
initctl start sphinxsearch
initctl status sphinxsearch

# For init.d:
/etc/init.d/sphinxsearch start
ps aux | grep searchd

If the service fails to start, check these critical points:

  • Verify all paths in your configuration match actual installation locations
  • Check permissions for the Sphinx data directory
  • Review /var/log/sphinxsearch/searchd.log for errors
  • Ensure the configuration file syntax is correct with searchd --config /path/to/sphinx.conf --check

While this solution targets Ubuntu 9.04, if you upgrade to systemd-based systems later, you'll need to create a .service file:

[Unit]
Description=Sphinx Search Service
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf
ExecStop=/usr/local/sphinx/bin/searchd --config /usr/local/sphinx/etc/sphinx.conf --stop
PIDFile=/usr/local/sphinx/var/log/searchd.pid
Restart=on-failure

[Install]
WantedBy=multi-user.target

When dealing with service management on Ubuntu servers, you need to understand whether your system uses the traditional init.d scripts or the newer systemd approach. For Ubuntu 9.04 (which is quite old), you'll be working with init.d scripts.

First, let's create a proper init script in /etc/init.d/sphinxsearch:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          sphinxsearch
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Sphinx search server
# Description:       Sphinx search server daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/searchd
NAME=sphinxsearch
DESC="Sphinx search server"
PIDFILE=/var/run/searchd.pid
CONFIG=/etc/sphinx/sphinx.conf

test -x $DAEMON || exit 0

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile $PIDFILE \
        --exec $DAEMON -- --config $CONFIG
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
        --exec $DAEMON
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile $PIDFILE \
        --exec $DAEMON
    sleep 1
    start-stop-daemon --start --quiet --pidfile $PIDFILE \
        --exec $DAEMON -- --config $CONFIG
    echo "$NAME."
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
    exit 1
    ;;
esac

exit 0

After creating the file, you need to make it executable and set up the proper symlinks:

sudo chmod +x /etc/init.d/sphinxsearch
sudo update-rc.d sphinxsearch defaults

To test if everything works correctly:

sudo service sphinxsearch start
sudo service sphinxsearch status

If you encounter problems, check these common solutions:

1. Verify paths in the init script match your actual installation
2. Check permissions on the PID file directory
3. Ensure the sphinx configuration file exists at the specified location

For modern Ubuntu systems (15.04 and later), you would create a systemd service file instead:

[Unit]
Description=Sphinx Search Server
After=network.target

[Service]
Type=forking
PIDFile=/var/run/searchd.pid
ExecStart=/usr/bin/searchd --config /etc/sphinx/sphinx.conf
ExecStop=/usr/bin/searchd --stop --config /etc/sphinx/sphinx.conf
Restart=on-failure

[Install]
WantedBy=multi-user.target

Then enable it with:

sudo systemctl enable sphinxsearch.service