1#!/bin/bash
2#
3#/**
4# * Copyright The Apache Software Foundation
5# *
6# * Licensed to the Apache Software Foundation (ASF) under one
7# * or more contributor license agreements.  See the NOTICE file
8# * distributed with this work for additional information
9# * regarding copyright ownership.  The ASF licenses this file
10# * to you under the Apache License, Version 2.0 (the
11# * "License"); you may not use this file except in compliance
12# * with the License.  You may obtain a copy of the License at
13# *
14# *     http://www.apache.org/licenses/LICENSE-2.0
15# *
16# * Unless required by applicable law or agreed to in writing, software
17# * distributed under the License is distributed on an "AS IS" BASIS,
18# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19# * See the License for the specific language governing permissions and
20# * limitations under the License.
21# */
22
23set -e -u -o pipefail
24
25SCRIPT_NAME=${0##*/}
26SCRIPT_DIR=$(cd `dirname $0` && pwd )
27
28print_usage() {
29  cat >&2 <<EOT
30Usage: $SCRIPT_NAME <options>
31Options:
32  --kill
33    Kill local process-based HBase cluster using pid files.
34  --show
35    Show HBase processes running on this machine
36EOT
37  exit 1
38}
39
40show_processes() {
41  ps -ef | grep -P "(HRegionServer|HMaster|HQuorumPeer) start" | grep -v grep
42}
43
44cmd_specified() {
45  if [ "$CMD_SPECIFIED" ]; then
46    echo "Only one command can be specified" >&2
47    exit 1
48  fi
49  CMD_SPECIFIED=1
50}
51
52list_pid_files() {
53  LOCAL_CLUSTER_DIR=$SCRIPT_DIR/../../target/local_cluster
54  LOCAL_CLUSTER_DIR=$( cd $LOCAL_CLUSTER_DIR && pwd )
55  find $LOCAL_CLUSTER_DIR -name "*.pid"
56}
57
58if [ $# -eq 0 ]; then
59  print_usage
60fi
61
62IS_KILL=""
63IS_SHOW=""
64CMD_SPECIFIED=""
65
66while [ $# -ne 0 ]; do
67  case "$1" in
68    -h|--help)
69      print_usage ;;
70    --kill)
71      IS_KILL=1
72      cmd_specified ;;
73    --show)
74      IS_SHOW=1
75      cmd_specified ;;
76    *)
77      echo "Invalid option: $1" >&2
78      exit 1
79  esac
80  shift
81done
82
83if [ "$IS_KILL" ]; then
84  list_pid_files | \
85    while read F; do
86      PID=`cat $F`
87      echo "Killing pid $PID from file $F"
88      # Kill may fail but that's OK, so turn off error handling for a moment.
89      set +e
90      kill -9 $PID
91      set -e
92    done
93elif [ "$IS_SHOW" ]; then
94  PIDS=""
95  for F in `list_pid_files`; do
96    PID=`cat $F`
97    if [ -n "$PID" ]; then
98      if [ -n "$PIDS" ]; then
99        PIDS="$PIDS,"
100      fi
101      PIDS="$PIDS$PID"
102    fi
103  done
104  ps -p $PIDS
105else
106  echo "No command specified" >&2
107  exit 1
108fi
109
110
111