You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by or...@apache.org on 2017/10/19 11:01:46 UTC
qpid-broker-j git commit: QPID-7910: [Java Broker] Improve qpid stop
script
Repository: qpid-broker-j
Updated Branches:
refs/heads/master 2318c2fe1 -> 331c7276a
QPID-7910: [Java Broker] Improve qpid stop script
Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/331c7276
Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/331c7276
Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/331c7276
Branch: refs/heads/master
Commit: 331c7276aadae28ae046ca0b5165934d47d72720
Parents: 2318c2f
Author: Alex Rudyy <or...@apache.org>
Authored: Thu Oct 19 12:00:00 2017 +0100
Committer: Alex Rudyy <or...@apache.org>
Committed: Thu Oct 19 12:01:36 2017 +0100
----------------------------------------------------------------------
broker/bin/qpid.stop | 207 ++++++++++++++++------------------------------
1 file changed, 72 insertions(+), 135 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/331c7276/broker/bin/qpid.stop
----------------------------------------------------------------------
diff --git a/broker/bin/qpid.stop b/broker/bin/qpid.stop
index 867f978..b27dd87 100755
--- a/broker/bin/qpid.stop
+++ b/broker/bin/qpid.stop
@@ -18,12 +18,14 @@
# under the License.
#
-# qpid.stop Script
-#
-# Script checks for a given pid running DEFAULT_SEARCH and attempts to quit it
+# qpid.stop script
#
+# The script can be used to stop instances of Qpid Brokers with given PIDs or all running Qpid Brokers for current user
+# if no PID is provided.
+# The script issues KILL TERM for found or specified Qpid Broker instances and waits for their termination
+# for up to MAX_WAIT_PERIOD seconds.
-MAX_ATTEMPTS=2
+MAX_WAIT_PERIOD=3600
SLEEP_DELAY=1
DEFAULT_SEARCH="PNAME=QPBRKR"
@@ -33,146 +35,81 @@ else
SEARCH=$QPID_STOP_SEARCH;
fi
-#
-# Forcably Quit the specified PID($1)
-#
-forceQuit()
-{
-kill -9 $1
-}
-
-#
-# Gracefully ask the PID($1) to quit
-#
-quit()
-{
-kill $1
-}
-
-#
-# grep for the session ID ($1) and return 0 for successful quit and 1 for process alive
-#
-lookup_pid()
+shudown_brokers()
{
-result=`ps -e | grep $1 | wc -l`
-}
-
-#
-# grep ps for all instances of $SEARCH for the current user and collect PIDs
-#
-lookup_all_pids()
-{
-pids=`pgrep -f -U $USER $SEARCH`
-result_all=`echo -n $pids | wc -w`
-}
-
-#
-# check that the PID passed in is for a Qpid broker owned by this user and alive
-#
-validate_pid()
-{
-result=`pgrep -fl $SEARCH | grep $1 | wc -l`
-}
-
-#
-# Show the PS output for given set of pids
-#
-showPids()
-{
-ps -o user,pid,args -p $pids
-}
-
-#
-# Sleep and then check then lookup the PID($1) to ensure it has quit
-#
-check()
-{
-echo "Waiting $SLEEP_DELAY second for $1 to exit"
-sleep $SLEEP_DELAY
-lookup_pid $1
-}
-
-#
-# Verify the PID($1) is available
-#
-verifyPid()
-{
-validate_pid $1
-if [[ $[$result] == 1 ]] ; then
- brokerspid=$1
-else
- echo "Unable to locate Qpid Broker Process with PID $1. Check PID and try again."
- exit -1
-fi
-}
-
-#
-# Stops all Qpid brokers for current user
-#
-qpid_stopall_brokers()
-{
-for pid in $pids ; do
- lookup_pid $pid;
- brokerspid=$pid;
- stop_broker $pid;
-done
-}
-
-#
-# Stops Qpid broker with brokerspid id
-#
-stop_broker()
-{
-# Attempt to quit the process MAX_ATTEMPTS Times
-attempt=0
-while [[ $[$result] > 0 && $[$attempt] < $[$MAX_ATTEMPTS] ]] ; do
- quit $brokerspid
- check $brokerspid
- attempt=$[$attempt + 1]
-done
+ pids=( "$@" )
+ if [[ ${#pids[@]} == 1 ]]; then
+ echo "Found Qpid Broker process with the PID '${pids[@]}'"
+ else
+ echo "Found Qpid Broker processes with PIDs: ${pids[@]}"
+ fi
-# Check that it has quit
-if [[ $[$result] == 0 ]] ; then
- echo "Process quit"
-else
+ declare -a monitored_pids=()
+ for pid in "${pids[@]}"
+ do
+ echo "Killing Qpid Broker with the PID '$pid'"
+ if kill $pid 2>/dev/null; then
+ monitored_pids+=($pid)
+ else
+ echo "Cannot shutdown Qpid Broker with the PID '$pid'"
+ fi
+ done
- attempt=0
- # Now attempt to force quit the process
- while [[ $[$result] > 0 && $[$attempt] < $[$MAX_ATTEMPTS] ]] ; do
- forceQuit $brokerspid
- check $brokerspid
- attempt=$[$attempt + 1]
+ echo "Waiting for up to $MAX_WAIT_PERIOD seconds for process(es) to terminate..."
+ end_time=$(($SECONDS+$MAX_WAIT_PERIOD))
+ while [[ "${#monitored_pids[@]}" -ne 0 && "$SECONDS" -lt "$end_time" ]]
+ do
+ sleep $SLEEP_DELAY
+ declare -a running_pids=()
+ for pid in "${monitored_pids[@]}"
+ do
+ if kill -0 $pid 2>/dev/null; then
+ running_pids+=($pid)
+ fi
+ done
+ declare -a monitored_pids=()
+ monitored_pids=("${running_pids[@]}")
done
- # Output final status
- if [[ $[$result] > 0 && $[$attempt] == $[$MAX_ATTEMPTS] ]] ; then
- echo "Stopped trying to kill process: $brokerspid"
- echo "Attempted to stop $attempt times"
+ if [[ "${#monitored_pids[@]}" != 0 ]]; then
+ echo "Process(es) with PID(s) ${monitored_pids[@]} not terminated within ${MAX_WAIT_PERIOD} seconds. Please, investigate..."
+ exit 1
else
- echo "Done "
+ echo "Qpid Broker process(es) terminated successfully"
fi
-fi
-
}
-#
-# Main Run
-#
-
-# Check if we are killing all qpid pids or just one.
-# Now uses local function qpid_stopall_brokers
-if [[ $# == 0 ]] ; then
- lookup_all_pids
- if [[ $[$result_all] > 0 ]] ; then
- echo "Killing All Qpid Brokers for user: '$USER'"
- qpid_stopall_brokers
+main()
+{
+ arg_length=$#
+ if [[ $arg_length == 0 ]] ; then
+ pids=($(pgrep -f -U $USER $SEARCH))
+
+ if [[ ${#pids[@]} == 0 ]]; then
+ echo "No Qpid Broker process found running"
+ else
+ shudown_brokers "${pids[@]}"
+ fi
+ elif [[ $arg_length == 1 && "$1" == "-h" ]] ; then
+ echo "$0: script tries to stop instances of Qpid Brokers with given PIDs or all running Qpid Brokers if no PID is provided."
+ echo "usage: $0 [pid...]"
else
- echo "No Qpid Brokers found running for user: " $USER
+ pids=( "$@" )
+ declare -a broker_pids=()
+ for pid in "${pids[@]}"
+ do
+ ps -f -p $pid | grep "$SEARCH" | grep java >/dev/null
+ if [[ "$?" == "0" ]]; then
+ broker_pids+=($pid)
+ else
+ echo "Qpid Broker with the PID '$1' is not found"
+ fi
+ done
+
+ if [[ ${#broker_pids[@]} != 0 ]]; then
+ shudown_brokers "${broker_pids[@]}"
+ fi
fi
- exit $result
-else
- verifyPid $1
- stop_broker
- exit $result
-fi
+}
+main "$@"
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org