Skip to content

Instantly share code, notes, and snippets.

@jaredgriffith
Created November 15, 2013 23:27
Show Gist options
  • Save jaredgriffith/7493444 to your computer and use it in GitHub Desktop.
Save jaredgriffith/7493444 to your computer and use it in GitHub Desktop.
Solr init.d script (works on RHEL 6)
#!/bin/bash
### BEGIN INIT INFO
# Provides: solr
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# X-Interactive: true
# Short-Description: Start/stop solr web server
### END INIT INFO
#
# Solr startup script
#
# chkconfig: 2345 80 05
# description: Solr
# Source function library.
if [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
fi
if [ -f /lib/lsb/init-functions ]; then
. /lib/lsb/init-functions
fi
if [ -f /etc/sysconfig/solr ]; then
. /etc/sysconfig/solr
fi
prog=solr
RETVAL=0
# Grab UID of start user. Abort loudly if it doesn't exist.
STARTUID=`id -u ${STARTUSER} 2> /dev/null`
RET=$?
if [ ${RET} -ne 0 ];
then
echo "User ${STARTUSER} does not exist."
exit 1
fi
# If we're root, restart as unpriveleged user. Abort if anyone else.
if [ ${UID} -eq 0 ];
then
exec su ${STARTUSER} -c "$0 $*"
elif [ ${UID} -eq ${STARTUID} ];
then
echo "do nothing" > /dev/null 2> /dev/null
else
echo "This needs to be run as root or ${STARTUSER}. "
exit 1
fi
runexit() {
echo "Already running!"
exit 1
}
start() {
echo -n "Starting Solr... "
if [ "x${PID}" == "x" ];
then
PID=0
fi
# Let's check to see if the PID is up and is actually Solr
checkproccmdline > /dev/null 2> /dev/null
if [ ${RET} -eq 0 ];
then
# If PID is up and is Solr, fail.
runexit
fi
# Pre-emptive extreme prejudice strike on anything using Solr's port
killbyport
sleep 1
# Start 'er up.
cd ${SOLRROOT}
#${JBIN} ${STARTARGS} -jar ${SOLRROOT}/start.jar >${SOLRLOG}/solr.out 2>${SOLRLOG}/solr_err.log &
${JBIN} ${STARTARGS} -jar start.jar >${SOLRLOG}/solr.out 2>${SOLRLOG}/solr_err.log &
PID=$!
if [ $PID ];
then
echo ${PID} > ${PIDFILE}
disown ${PID}
#echo "done"
echo_success
else
echo_failure
fi
echo
return $RETVAL
}
checkproccmdline() {
grep ${PROC_CHECK_ARG} /proc/${PID}/cmdline > /dev/null 2> /dev/null
RET=$?
}
termbypid() {
checkpid ${PID}
RET=$?
if [ ${RET} -eq 0 ];
then
checkproccmdline
if [ ${RET} -eq 0 ];
then
kill $PID
else
echo
echo "pid ${PID} is not Solr!"
echo > ${PIDFILE}
fi
fi
}
killbypid() {
checkpid ${PID}
RET=$?
if [ ${RET} -eq 0 ];
then
checkproccmdline
if [ ${RET} -eq 0 ];
then
kill -9 $PID
else
echo
echo "pid ${PID} is not Solr!"
echo > ${PIDFILE}
fi
fi
}
termbyport() {
${FUSER} -sk -n tcp ${LISTENPORT}
}
killbyport() {
${FUSER} -sk9 -n tcp ${LISTENPORT}
}
checkpidsleep() {
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
checkpid ${PID} && sleep 1
}
stop() {
echo -n "Stopping Solr... "
# check to see if PID is empty.
if [ "x${PID}" == "x" ];
then
# term then kill anything using Solr's port, assume success.
termbyport
sleep 5
killbyport
else
# Try Jetty's stop mechanism.
${JBIN} $STOPARGS -jar /${SOLRROOT}/start.jar --stop \
> /dev/null 2> /dev/null
checkpidsleep
# Controlled death by PID.
termbypid
checkpidsleep
# Controlled death by Solr port.
termbyport
checkpidsleep
# Sudden death by PID and Solr port.
killbyport
killbypid
checkpidsleep
# report failure if none of the above worked.
if checkpid $PID 2>&1;
then
echo_failure
fi
fi
# echo "done"
echo > $PIDFILE
echo_success
echo
return $RETVAL
}
UP=`cat /proc/uptime | cut -f1 -d\ | cut -f1 -d.`
# If the machine has been up less than 3 minutes, wipe the pidfile.
if [ ${UP} -lt 180 ];
then
echo > ${PIDFILE}
fi
PID=`cat ${PIDFILE}`
solrstatus() {
if [ ${PID} ];
then
echo "Solr is running with pid $PID"
else
echo "Solr is not running"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
solrstatus
;;
restart)
stop
start
;;
*)
echo $"Usage: $0 {start|stop|status|restart}"
exit 1
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment