Apache Solr init Script [Start|Stop|Restart]

Apache Solr Init Script

Overview

In this article, I have demonstrated the init script for solr service. This script is used to start, stop and restart solr service.

By default, Solr will not provide you init script or not install script on system.
This script is tested on Debian Operating system but should work elsewhere without facing any issues.

In my previous article, i have explained about the integration of Apache Solr with Magento. If you have not yet installed apache Solr or you don’t have any idea about installation of Apache Solr refer below link,

Read: Installation & Configuration of Apache Solr with Magento.

Now come to the point, before using below script on the server. Make sure you have install below dependency package. This package helps to run Solr service.

sudo apt-get install daemon

Once installation of Daemon complete follows below simple step to setup init script.

Step 1: Copy Init Script

Copy below provided init script and create new file “solr” under “/etc/init.d/” directory. Paste below script in solr file. Change the directory path which is in bold with the apache-solr path as per your requirement .

vim /etc/init.d/solr

#!/bin/sh

start () {
echo -n "Starting solr..."
# Start daemon
daemon --chdir='/opt/apache-solr/example' --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 2: Permissions

Once the script is saved in init.d directory. You need to give executable permission to the script.

chmod +x /etc/init.d/solr
chown root:root /etc/init.d/solr

Step 3: Test Script

Run below command to test script is running without issues.

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

If you’re facing any difficulties while configuring init script please, do comment your queries or problem via the comment section, till then stay tuned to techthings.org for more such valuable articles.

Useful Scripts :

Latest Comments
  1. Dlightdaily November 19, 2016

Leave a Reply

Your email address will not be published. Required fields are marked *