Attached is a startup script for RHEL / CentOS that will start your Apache Solr server at startup and cleanly shut it down at shutdown.

Download the script attached to this page, rename it to "solr", and put it at /etc/init.d/solr.

Edit the line starting with SOLR_DIR to make sure it's pointing to the location where your solr installation lives.

Make the script executable:

sudo chmod a+rx /etc/init.d/solr

Now install the script using chkconfig:

sudo chkconfig --add solr

This will add the script to runlevels 3 and 5, so solr will start once the system is up. Check to make sure that things are properly set up by typing

chkconfig --list

You should see solr among the various services. You should also be able to use the service command to stop and start solr:

sudo service solr stop
Stopping Solr
sudo service solr start
Starting Solr
sudo service solr restart
Stopping Solr
Starting Solr

Comments

geerlingguy’s picture

Another note: If you start getting the error below, you'll need to add a minimum memory value to the script you place in /etc/init.d/solr

Here's the error:

Error occurred during initialization of VM
Could not reserve enough space for object heap

Here's the fix (in the JAVA_OPTIONS line):

JAVA_OPTIONS="-Xms128m -Xmx256m -DSTOP.PORT=8079 -DSTOP.KEY=mustard -jar start.jar"

You can change the Xms (minimum) or Xmx (maximum) memory values as you see fit. These memory values work great on my server running two drupal sites that get moderate traffic and only a few searches /new nodes per hour.

__________________
Personal site: www.jeffgeerling.com

zazinteractive’s picture

Thanks. I had this problem and your suggestions works

pamster’s picture

Great tutorial

protoplasm’s picture

I have followed the instructions above exactly and I get a screen that says Starting and/or Stopping solr service. But it never goes through all the printout like it does when the commands are typed manually java -jar start.jar at the apache-solr/example prompt. Permissions of the folder are 755 and I tried changing ownership and moving the example directory around, but no luck. I'm using apache 2.2.14 on centos. Any ideas would be greatly appreciated.

I can manually get this to work without the script--just can't get it to work automatically or manually via sudo service solr start.
Thanks--

Ok, I finally got this script to work. The problem was the path to java. Using whereis--I was trying usr/share/java since usr/bin/java didn't exist. oops.
The location of my java was /usr/local/jdk1.6.0_17/jre/bin/java.

protoplasm

protoplasm’s picture

This works manually to start and stop, but I can't get it to work automatically to start apache solr on an apache automagic restart on a Centos apache 2.2 server. Any suggestions appreciated.

UPDATE: Finally got this to work automatically on an apache reboot. I believe the problem was related to the file owner of where I placed the solr distribution with the example file. When I placed it in a location (opt) that had root ownership, then it worked automatically with apache reboot.

protoplasm

Jax’s picture

The example starts the solr service under the root user. How to get it running by another user? If I use sudo it works when starting it manually but it fails on reboot since there is no terminal available.

alanthing’s picture

You can edit your sudoers file to allow your solr (or other) user to not require a tty: http://serverfault.com/questions/111064/sudoers-how-to-disable-requirett...

naxoc’s picture

On my CentOS box the chkconfig and service commands are not in path, so I had to supply the full path. If you run into trouble having messages like sudo: chkconfig: command not found, simply use the full path:

sudo /sbin/chkconfig --add solr
/sbin/chkconfig --list
sudo /sbin/service solr stop

Other than that this works like a charm :)

tsbah’s picture

This is a great resource. I had to create the /var/solr directory and the /var/log/solr file.

After that, everything went fine. Now I can issue the service commands [start|stop|restart]. A fourth option, available with the httpd daemon, is status. It'd be nice to have it with the Solr server.

Thanks

aerodub’s picture

if the above message appears on your CentOs box
add the following lines after bin/bash

# chkconfig: 2345 95 20
# description: Some description
# What your script does (not sure if this is necessary though)
# processname: myscript

for more see http://stackoverflow.com/questions/2150767/how-to-start-solr-automatically

DanielFbrg’s picture

thanks, you saved my day

johnhanley’s picture

If the message "bad interpreter: No such file or directory" appears when attempting to execute the attached script, you need to change the file format by following the instructions found here:

http://www.gizmola.com/blog/archives/87-Linux-shell-scripting-bad-interp...

webadpro’s picture

Thanks. That did help :)

xurubo93’s picture

When you are using the above attached script with this options
JAVA_OPTIONS="-Xmx1024m -DSTOP.PORT=8079 -DSTOP.KEY=mustard -jar start.jar"
and you have iptables configured on your system you will not be able to stop it with
/etc/init.d/solr stop
because iptables will block port 8079 on the localhost.

You have to extend your iptables-configuration with
iptables -A TCP -p tcp -m tcp -s localhost -d localhost --dport 8079 -j ACCEPT
or similar to that regarding the iptables-config you are using.

gswiss’s picture

Should the script be named solr or solr.sh? No file extension?

//G

gswiss’s picture

Hi,

I have followed the instructions exactly on how to get Solr to start automatically on CentOS and am still stuck. I am on Drupal 6 with Solr 1.4.1. Solr works fine. But when the machine is rebooted or Apache is bounced, it does not start uop automatically -- I must do start -java start.jar every single time. But I can't stop it to test the script.

My java path is correct and I have not changed the script at all, aside from adding /drupal to the path for the start.jar file. Here it is:

#!/bin/sh

# chkconfig: 2345 95 20
# description: Starts, stops, restarts Apache Solr...

SOLR_DIR="/apache-solr-1.4.1/drupal"
JAVA_OPTIONS="-Xmx1024m -DSTOP.PORT=8079 -DSTOP.KEY=mustard -jar drupal/start.jar"
LOG_FILE="/var/log/solr.log"
JAVA="/usr/bin/java"

case $1 in
    start)
        echo "Starting Solr...."
        cd $SOLR_DIR
        $JAVA $JAVA_OPTIONS 2> $LOG_FILE &
        ;;
    stop)
        echo "Stopping Solr...."
        cd $SOLR_DIR
        $JAVA $JAVA_OPTIONS --stop
        ;;
    restart)
        $0 stop
        sleep 1
        $0 start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}" >&2
        exit 1
        ;;
esac

I have registered the service using chkconfig and it appears in the chkconfig --list. It also has the correct permissions. However, I cannot STOP Solr! When I issue either:

service solr stop
/etc/init.d/solr stop

I get this error:

Stopping Solr....
java.net.ConnectException: Connection refused
        at java.net.PlainSocketImpl.socketConnect(Native Method)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:327)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:193)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:385)
        at java.net.Socket.connect(Socket.java:546)
        at java.net.Socket.connect(Socket.java:495)
        at java.net.Socket.<init>(Socket.java:392)
        at java.net.Socket.<init>(Socket.java:235)
        at org.mortbay.start.Main.stop(Main.java:526)
        at org.mortbay.start.Main.main(Main.java:104)

I also have nothing configured in iptables, FYI. Traffic through 8079 should be fine. Anyone have any idea what is happening here?

If I remove "/drupal" from SOLR_DIR then I was getting "Unable to access start.jar." That's why I added it.

Thanks,
Garrett

arnoldbird’s picture

Same problem here. "Connection refused."