Run Solr as a service in Ubuntu

I’ve been doing some tests with Nutch and Solr these days. One thing I didn’t like was that when you run Solr all the info messages output to the standard output in the command line.

solr

I rather use it as a service so after a little search I found this solution from Terrance A. Snyder. I’ve just tweaked a little bit to get the output and error logs to different files and I’m still using the example folder.

#!/bin/sh -e
# SOLR auto-start
#
# description: auto-starts solr engine
# processname: solr
# pidfile: /var/run/solr.pid

NAME="solr"
PIDFILE="/var/run/solr.pid"
LOG_FILE="/var/log/solr-output.log"
ERROR_FILE="/var/log/solr-error.log"
SOLR_DIR="/opt/solr-4.10.0/example"
JAVA_OPTIONS="-Xmx1024m -DSTOP.PORT=8079 -DSTOP.KEY=stopkey -jar start.jar"
JAVA="/usr/bin/java"

start() {
  echo -n "Starting $NAME... "
  if [ -f $PIDFILE ]; then
    echo "is already running!"
  else
    cd $SOLR_DIR
    $JAVA $JAVA_OPTIONS 1> $LOG_FILE 2> $ERROR_FILE &
    sleep 2
    echo `ps -ef | grep -v grep | grep java | awk '{print $2}'` > $PIDFILE
    echo "(Done)"
  fi
  return 0
}

stop() {
  echo -n "Stopping $NAME... "
  if [ -f $PIDFILE ]; then
    cd $SOLR_DIR
    $JAVA $JAVA_OPTIONS --stop
    sleep 2
    rm $PIDFILE
    echo "(Done)"
  else
    echo "can not stop, it is not running!"
  fi
  return 0
}

case "$1" in
  start)
    start
  ;;
  stop)
    stop
  ;;
  restart)
    stop
    sleep 5
    start
  ;;
  *)
    echo "Usage: $0 (start | stop | restart)"
    exit 1
  ;;
esac

Save this as /etc/init.d/solr, give it execution permissions (sudo chmod +x /etc/init.d/solr) and then you are ready to start and stop it as usual.

sudo /etc/init.d/solr start
sudo /etc/init.d/solr stop

You can also configure it to start with the system:

sudo update-rc.d solr defaults

And remove it:

sudo update-rc.d solr remove

Ref: http://blog.shutupandcode.net/?p=463

You might also like

Install MongoDB in Ubuntu 12.04 with PHP driver
Install MongoDB:sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 echo...

Create a bootable Ubuntu USB drive from an ISO in Ubuntu
The first thing we need to do is download an Ubuntu ISO file. The second one is plugin the USB drive...

Shrink MongoDB journal files
Trying to get some space in my harddrive I noticed that the journal files of MongoDB where around 3GB.Fixing...

ARToolKit with OpenKinect using the IR camera
More than one year ago I started adding Kinect support to ARToolKit using libfreenect. I had some results...

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.