Using Solr with Upstart instead of init.d

Posted on March 3, 2011 by anthony

You can use the default Solr (lucene.apache.org/solr/) examples right out of the box with Upstart to emulate a working daemon on your servers. Unpack the Solr download anywhere on your server and clone the “example” directory to a unique folder else where in your file system. I choose “/opt/solr/solr-anthony” for example (now known as SOLR_HOME). Inside of SOLR_HOME you will see a start.jar file, this is Jetty (jetty.codehaus.org/jetty/). Jetty is a Java servlet container and is a good alternative to using Tomcat or other heavy containers. So how do you get this to work with Upstart? Very easily I say!

If you are on Debian/Ubuntu/Fedora this will work for you no problem. Inside of ‘/etc/init’ you can create init scripts that Upstart reacts upon. Things like ‘service apache2 restart’ do a look up in this directory find the right script and apply the logic necessary to the daemon. In ‘/etc/init’ I placed a file called ‘solr-anthony.conf’. The trick to the naming convention is that whatever you name your configuration file that will be the name of the service sans the ‘.conf’ extension. For example if you create ‘foobar.conf’ in ‘/etc/init’ then ‘service foobar stop’ would be a valid command. So now that you have created the file, what are its contents? As follows:

description	"Solr Search Server"
 
# Make sure the file system and network devices have started before
# we begin the daemon
start on (filesystem and net-device-up IFACE!=lo)
 
# Stop the event daemon on system shutdown
stop on shutdown
 
# Respawn the process on unexpected termination
respawn
 
# The meat and potatoes
exec /usr/bin/java -Xms128m -Xmx256m -Dsolr.solr.home=/opt/solr/solr-anthony/solr -Djetty.home=/opt/solr/solr-anthony -jar /opt/solr/solr-anthony/start.jar >> /var/log/solr-anthony.log 2>&1

With Solr there is an important configuration that you must pass in and that is Solr’s home directory, if you look inside of the default ‘example’ folder from Apache you will find the ‘solr’ directory, this is it. Additionally through trial and error (I am not a Java developer…) I found that you have to set the Jetty home directory as well or else there are certain files that the JVM will not know exist. The Jetty home directory is where the ‘start.jar’ file lives.

Optionally I hard coded my path to the Java executable, it is not a bad idea, of course you can parse that out in bash if necessary using the ‘which’ command. Also I set the amount of memory the JVM can start with and the amount of memory that the JVM can consume at a maximum for this application. If you want to log Jetty or the Solr application I send all output via redirection to /var/log/solr-anthony.log. ’2>&1′ in Linux means route stderr to where stdout is going.

This entry was posted in Java, Linux, Solr, Upstart and tagged solr java upstart. Bookmark the permalink.

Comments are closed.