Apache Solr open-source search software
SOLR-6529: Stop command in the start scripts should only stop the instance that it had started
Timothy Potter98e1abe5324c
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 5343a810e3d..a3851c7dcb0 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -297,6 +297,9 @@ Other Changes* SOLR-6549: bin/solr script should support a -s option to set the -Dsolr.solr.home property.(Timothy Potter)+* SOLR-6529: Stop command in the start scripts should only stop the instance that it had started.+ (Varun Thacker, Timothy Potter)+================== 4.10.1 ==================Bug Fixesdiff --git a/solr/bin/solr b/solr/bin/solrindex d48e9d3ee89..eaaf7163c51 100755--- a/solr/bin/solr+++ b/solr/bin/solr@@ -48,6 +48,7 @@ SOLR_SCRIPT="$0"verbose=falseTHIS_OS=`uname -s`hasLsof=$(which lsof)+stop_all=false# for now, we don't support running this script from cygwin due to problems# like not having lsof, ps waux, curl, and awkward directory handling@@ -189,6 +190,8 @@ function print_usage() {echo ""echo " -p <port> Specify the port the Solr HTTP listener is bound to; default is 8983"echo ""+ echo " -all Find and stop all running Solr servers on this host"+ echo ""echo " -V Verbose messages from this script"echo ""echo "NOTE: If port is not specified, then all running Solr servers are stopped."@@ -220,6 +223,19 @@ spinner()printf " \b\b\b\b"}+# given a port, find the pid for a Solr process+function solr_pid_by_port() {+ THE_PORT="$1"+ if [ -e "$SOLR_TIP/bin/solr-$THE_PORT.pid" ]; then+ PID=`cat $SOLR_TIP/bin/solr-$THE_PORT.pid`+ CHECK_PID=`ps waux | awk '{print $2}' | grep $PID | sort -r | tr -d ' '`+ if [ "$CHECK_PID" != "" ]; then+ local solrPID=$PID+ fi+ fi+ echo "$solrPID"+}+# extract the value of the -Djetty.port parameter from a running Solr processfunction jetty_port() {SOLR_PID="$1"@@ -254,11 +270,12 @@ function run_tool() {# get information about any Solr nodes running on this hostfunction get_info() {# first, see if Solr is running- numSolrs=`ps waux | grep java | grep start.jar | wc -l | sed -e 's/^[ \t]*//'`+ numSolrs=`find $SOLR_TIP/bin -name "solr-*.pid" -type f | wc -l | tr -d ' '`if [ "$numSolrs" != "0" ]; thenecho -e "\nFound $numSolrs Solr nodes: "- for ID in `ps waux | grep java | grep start.jar | awk '{print $2}' | sort -r`+ for PIDF in `find $SOLR_TIP/bin -name "solr-*.pid" -type f`do+ ID=`cat $PIDF`port=`jetty_port "$ID"`if [ "$port" != "" ]; thenecho ""@@ -281,27 +298,30 @@ function stop_solr() {SOLR_PORT="$2"STOP_PORT="79${SOLR_PORT: -2}"STOP_KEY="$3"-- SOLR_PID=`ps waux | grep start.jar | grep $SOLR_PORT | grep -v grep | awk '{print $2}' | sort -r`+ SOLR_PID="$4"+if [ "$SOLR_PID" != "" ]; thenecho -e "Sending stop command to Jetty stop port $STOP_PORT ... waiting 5 seconds to allow process $SOLR_PID to stop gracefully."$JAVA -jar $DIR/start.jar STOP.PORT=$STOP_PORT STOP.KEY=$STOP_KEY --stop || true(sleep 5) &spinner $!+ rm -f $SOLR_TIP/bin/solr-$SOLR_PORT.pidelseecho -e "No Solr nodes found to stop."exit 0fi- SOLR_PID=`ps waux | grep start.jar | grep $SOLR_PORT | grep -v grep | awk '{print $2}' | sort -r`- if [ "$SOLR_PID" != "" ]; then+ CHECK_PID=`ps waux | awk '{print $2}' | grep $SOLR_PID | sort -r | tr -d ' '`+ if [ "$CHECK_PID" != "" ]; thenecho -e "Solr process $SOLR_PID is still running; forcefully killing it now."kill -9 $SOLR_PIDecho "Killed process $SOLR_PID"+ rm -f $SOLR_TIP/bin/solr-$SOLR_PORT.pid+ sleep 1fi- SOLR_PID=`ps waux | grep start.jar | grep $SOLR_PORT | grep -v grep | awk '{print $2}' | sort -r`- if [ "$SOLR_PID" != "" ]; then+ CHECK_PID=`ps waux | awk '{print $2}' | grep $SOLR_PID | sort -r | tr -d ' '`+ if [ "$CHECK_PID" != "" ]; thenecho "ERROR: Failed to kill previous Solr Java process $SOLR_PID ... script fails."exit 1fi@@ -497,6 +517,10 @@ if [ $# -gt 0 ]; thenverbose=trueshift;;+ -all)+ stop_all=true+ shift+ ;;--)shiftbreak@@ -635,42 +659,32 @@ if [ "$EXAMPLE" != "" ]; thenesacfi-if [ "$SOLR_HOME" == "" ]; then- SOLR_HOME="$SOLR_SERVER_DIR/solr"-else- if [[ $SOLR_HOME != /* ]] && [[ -d "$SOLR_SERVER_DIR/$SOLR_HOME" ]]; then- SOLR_HOME="$SOLR_SERVER_DIR/$SOLR_HOME"- fi-fi--if [ ! -e "$SOLR_HOME" ]; then- echo -e "\nSolr home directory $SOLR_HOME not found!\n"- exit 1-fi-if [ ! -e "$SOLR_HOME/solr.xml" ]; then- echo -e "\nSolr home directory $SOLR_HOME must contain a solr.xml file!\n"- exit 1-fi--if [ "$STOP_KEY" == "" ]; thenSTOP_KEY="solrrocks"fi# stop all if no port specifiedif [[ "$SCRIPT_CMD" == "stop" && "$SOLR_PORT" == "" ]]; then- numSolrs=`ps waux | grep java | grep start.jar | wc -l | sed -e 's/^[ \t]*//'`- if [ "$numSolrs" != "0" ]; then- echo -e "\nFound $numSolrs Solr nodes to stop."- for ID in `ps waux | grep java | grep start.jar | awk '{print $2}' | sort -r`+ if $stop_all; then+ none_stopped=true+ for PIDF in `find $SOLR_TIP/bin -name "solr-*.pid" -type f`do- port=`jetty_port "$ID"`- stop_solr "$SOLR_SERVER_DIR" "$port" "$STOP_KEY"- done+ NEXT_PID=`cat $PIDF`+ port=`jetty_port "$NEXT_PID"`+ if [ "$port" != "" ]; then+ stop_solr "$SOLR_SERVER_DIR" "$port" "$STOP_KEY" "$NEXT_PID"+ none_stopped=false+ fi+ rm -f $PIDF+ done+ if $none_stopped; then+ echo -e "\nNo Solr nodes found to stop.\n"+ fielse- echo -e "\nNo Solr nodes found to stop.\n"+ echo -e "\nERROR: Must either specify a port using -p or -all to stop all Solr nodes on this host.\n"+ exit 1fi- exit+ exitfiif [ "$SOLR_PORT" == "" ]; then@@ -683,7 +697,7 @@ fiif [[ "$SCRIPT_CMD" == "start" ]]; then# see if Solr is already running- SOLR_PID=`ps waux | grep start.jar | grep $SOLR_PORT | grep -v grep | awk '{print $2}' | sort -r`+ SOLR_PID=`solr_pid_by_port "$SOLR_PORT"`if [ "$SOLR_PID" != "" ]; thenecho -e "\nSolr already running on port $SOLR_PORT (pid: $SOLR_PID)!"echo -e "Please use the 'restart' command if you want to restart this node.\n"@@ -691,7 +705,12 @@ if [[ "$SCRIPT_CMD" == "start" ]]; thenfielse# either stop or restart- stop_solr "$SOLR_SERVER_DIR" "$SOLR_PORT" "$STOP_KEY"+ SOLR_PID=`solr_pid_by_port "$SOLR_PORT"`+ if [ "$SOLR_PID" != "" ]; then+ stop_solr "$SOLR_SERVER_DIR" "$SOLR_PORT" "$STOP_KEY" "$SOLR_PID"+ else+ echo -e "No process found for Solr node running on port $SOLR_PORT"+ fifi# backup the log files@@ -715,6 +734,22 @@ if [ "$SCRIPT_CMD" == "stop" ]; thenfi# if we get here, then we're starting a new node up ...+if [ "$SOLR_HOME" == "" ]; then+ SOLR_HOME="$SOLR_SERVER_DIR/solr"+else+ if [[ $SOLR_HOME != /* ]] && [[ -d "$SOLR_SERVER_DIR/$SOLR_HOME" ]]; then+ SOLR_HOME="$SOLR_SERVER_DIR/$SOLR_HOME"+ fi+fi++if [ ! -e "$SOLR_HOME" ]; then+ echo -e "\nSolr home directory $SOLR_HOME not found!\n"+ exit 1+fi+if [ ! -e "$SOLR_HOME/solr.xml" ]; then+ echo -e "\nSolr home directory $SOLR_HOME must contain a solr.xml file!\n"+ exit 1+fi# if verbose gc logging enabled, setup the location of the log fileif [ "$GC_LOG_OPTS" != "" ]; then@@ -827,7 +862,7 @@ $SOLR_HOST_ARG -Djetty.port=$SOLR_PORT \$JAVA $SOLR_START_OPTS $SOLR_ADDL_ARGS -XX:OnOutOfMemoryError="$SOLR_TIP/bin/oom_solr.sh $SOLR_PORT" -jar start.jarelse# run Solr in the background- nohup $JAVA $SOLR_START_OPTS $SOLR_ADDL_ARGS -XX:OnOutOfMemoryError="$SOLR_TIP/bin/oom_solr.sh $SOLR_PORT" -jar start.jar 1>$SOLR_SERVER_DIR/logs/solr-$SOLR_PORT-console.log 2>&1 &+ nohup $JAVA $SOLR_START_OPTS $SOLR_ADDL_ARGS -XX:OnOutOfMemoryError="$SOLR_TIP/bin/oom_solr.sh $SOLR_PORT" -jar start.jar 1>$SOLR_SERVER_DIR/logs/solr-$SOLR_PORT-console.log 2>&1 & echo $! > $SOLR_TIP/bin/solr-$SOLR_PORT.pid# no lsof on cygwin thoughif [ "$hasLsof" != "" ]; then