blob: 58ccc805565915d150b35e4fa7168aa879ec0492 [file] [log] [blame]
#! /bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
### BEGIN INIT INFO
# Provides: solr-server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Apache Solr as a Service
### END INIT INFO
STATUS_RUNNING=0
STATUS_DEAD=1
STATUS_DEAD_AND_LOCK=2
STATUS_NOT_RUNNING=3
SERVICE_DESC="Apache Solr server"
SOLR_INSTALL_DIR="/usr/lib/solr"
if [ ! -d "$SOLR_INSTALL_DIR" ]; then
echo "$SOLR_INSTALL_DIR not found! Please check the SOLR_INSTALL_DIR setting in your $0 script."
exit 1
fi
# Path to an include file that defines environment specific settings to override default
# variables used by the bin/solr script. It's highly recommended to define this script so
# that you can keep the Solr binary files separated from live files (pid, logs, index data, etc)
# see bin/solr.in.sh for an example
SOLR_ENV="/etc/default/solr"
if [ ! -f "$SOLR_ENV" ]; then
echo "$SOLR_ENV not found! Please check the SOLR_ENV setting in your $0 script."
exit 1
fi
. $SOLR_ENV
[ "$SOLR_HOME" = "" ] && SOLR_HOME="/var/lib/solr"
[ "$SOLR_PORT" = "" ] && SOLR_PORT="8983"
[ "$SOLR_PID_DIR" = "" ] && SOLR_PID_DIR="/var/run/solr"
SOLR_PID_FILE="$SOLR_PID_DIR/solr-$SOLR_PORT.pid"
. /lib/lsb/init-functions
# Specify the user to run Solr as; if not set, then Solr will run as root.
# Running Solr as root is not recommended for production environments
RUNAS="solr"
# verify the specified run as user exists
runas_uid="`id -u "$RUNAS"`"
if [ $? -ne 0 ]; then
echo "User $RUNAS not found! Please create the $RUNAS user before running this script."
exit 1
fi
check_status()
{
status_of_proc -p $SOLR_PID_FILE $SOLR_INSTALL_DIR/bin/solrd "solr-server"
return $?
}
start_server()
{
start-stop-daemon --start --quiet --chuid $RUNAS:$RUNAS \
--exec $SOLR_INSTALL_DIR/bin/solrd -- start --config "$SOLR_ENV" > /dev/null 2>&1 || return 2
}
stop_server()
{
/bin/su -s /bin/bash -c \
"$SOLR_INSTALL_DIR/bin/solrd stop --config $SOLR_ENV" $SOLR_USER > /dev/null 2>&1 || return 2
# rm -f $SOLR_PID_FILE
}
export PATH="${PATH:+$PATH:}/usr/lib/solr/bin:/usr/sbin:/sbin"
case "$1" in
start)
check_status > /dev/null 2>&1
status=$?
if [ $status = 0 ]; then
log_success_msg "${SERVICE_DESC} is already running"
exit 0
fi
log_daemon_msg "Starting ${SERVICE_DESC} in cloud mode" || true
start_server
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
*) log_end_msg 1 ;;
esac
;;
stop)
check_status > /dev/null 2>&1
status=$?
if [ $status != 0 ]; then
log_success_msg "${SERVICE_DESC} is not running"
exit 1
fi
log_daemon_msg "Stopping ${SERVICE_DESC}" || true
stop_server
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
*) log_end_msg 1 ;;
esac
;;
restart)
log_daemon_msg "Restarting ${SERVICE_DESC}"
check_status > /dev/null 2>&1
status=$?
# stop running service
if [ $status = 0 ]; then
stop_server
if [ $? != 0 ]; then
log_end_msg 1
exit 1
fi
fi
# start service
start_server
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
*) log_end_msg 1 ;;
esac
;;
try-restart)
log_daemon_msg "Restarting ${SERVICE_DESC}"
check_status > /dev/null 2>&1
status=$?
if [ $status != 0 ]; then
log_failure_msg "${SERVICE_DESC} is not running"
exit 1
fi
stop_server
if [ $? != 0 ]; then
log_end_msg 1
exit 1
fi
# start service
start_server
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
*) log_end_msg 1 ;;
esac
;;
status)
status_of_proc -p $SOLR_PID_FILE $SOLR_INSTALL_DIR/bin/solrd "solr-server" && exit 0 || exit $?
;;
init)
check_status > /dev/null 2>&1
status=$?
if [ $status = 0 ]; then
log_daemon_msg "${SERVICE_DESC} is running"
log_end_msg 1
exit 1
fi
rm -rf ${SOLR_HOME}/*
/usr/bin/solrctl init --force
;;
*)
log_action_msg "Usage: /etc/init.d/solr-server {start|stop|restart|try-restart|status|init}" || true
exit 1
;;
esac
exit 0