Quantcast
Channel: Programming and Technology » GNU/Linux
Viewing all articles
Browse latest Browse all 55

Run Solr as a service in Ubuntu

$
0
0

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


Viewing all articles
Browse latest Browse all 55

Trending Articles