Mini Tutorial: Solr Startup Script

If you’re running Magento Enterprise Edition (and in my opinion anyone serious about eCommerce on Magento should be), one of the power features you get that isn’t available out of the box on the Community Edition is Solr support. As most of our clients choose to take advantage of this, I have spent a fair amount of time learning and setting up Solr on multiple systems. Depending on how you choose to run your system you may want to control starting and stopping Solr manually or (for the sake of this post) would rather have it run as an automatically restarting script.

Below is a quick Solr startup script, it has been tested in Ubuntu 12.04, but should work elsewhere without issues (thanks to user SuperMagic for the original script found on StackOverflow).

Step 1) install and setup Solr as usual

Step 2) copy you Solr instance to /usr/local/solr/MYSOLRINSTANCE (where MYSOLRINSTANCE is the name you have chosen for this instance of Solr)

Step 3) create the file /etc/init.d/solr and copy the following into it (be sure to change MYSOLRINSTANCE in the script below to match the directory name used in step 2)

#!/bin/sh

# Prerequisites:
# 1. Java must be installed (sudo apt-get install openjdk-6-jdk)
# 2. Solr needs to be installed at /usr/local/solr/MYSOLRINSTANCE (Replace MYSOLRINSTANCE with the name if directory you wish to use for your Solr instance)
# 3. daemon needs to be installed (sudo apt-get install daemon)
# 4. Script needs to be executed by root

# This script will launch Solr in a mode that will automatically re-spawn if it
# crashes. Output will be sent to /var/log/solr/solr.log. A PID file will be
# created in the standard location.

start () {
    echo -n "Starting solr..."

    # Start daemon (Replace MYSOLRINSTANCE with the name if directory you wish to use for your solr instance)
    daemon --chdir='/usr/local/solr/MYSOLRINSTANCE' --command "java -jar start.jar" --respawn --output=/var/log/solr/solr.log --name=solr --verbose

    RETVAL=$?
    if [ $RETVAL = 0 ]
    then
        echo "done."
    else
        echo "failed. See error code for more information."
    fi
    return $RETVAL
}

stop () {
    # Stop daemon
    echo -n "Stopping solr..."

    daemon --stop --name=solr  --verbose
    RETVAL=$?

    if [ $RETVAL = 0 ]
    then
        echo "Done."
    else
        echo "Failed. See error code for more information."
    fi
    return $RETVAL
}

restart () {
    daemon --restart --name=solr  --verbose
}

status () {
    # Report on the status of the daemon
    daemon --running --verbose --name=solr
    return $?
}

case "$1" in
    start)
        start
    ;;
    status)
        status
    ;;
    stop)
        stop
    ;;
    restart)
        restart
    ;;
    *)
        echo $"Usage: solr {start|status|stop|restart}"
        exit 3
    ;;
esac

exit $RETVAL

Step 4) Use one of the following commands to allow this script to start solr on reboot.

			chkconfig --add solr

Or (on Ubuntu):

			update-rc.d solr defaults

Or if your Linux distribution doesn’t have chkconfig (or update-rc.d):

			% ln -s /etc/init.d/solr /etc/rc3.d/S99solr

			% ln -s /etc/init.d/solr /etc/rc5.d/S99solr

			% ln -s /etc/init.d/solr /etc/rc3.d/K01solr

			% ln -s /etc/init.d/solr /etc/rc5.d/K01solr

That’s it, you can now start, stop, restart or check the status of your Solr instance using the following commands:

	% /etc/init.d/solr start
	% /etc/init.d/solr stop
	% /etc/init.d/solr restart
	% /etc/init.d/solr status