| # 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. |
| |
| NODE_DIR="${NODE_DIR}" |
| PID_DIR="$NODE_DIR/var/run" |
| LOG_DIR="$NODE_DIR/var/log" |
| |
| export HADOOP_CONF_DIR="$NODE_DIR/etc/hadoop/conf" |
| export HADOOP_PID_DIR=$NODE_DIR/var/run |
| |
| # Mark each process so they can be killed if needed. This is a safety mechanism for |
| # stopping the processes if the pid file has been removed for whatever reason. |
| export HADOOP_OPTS+=" -D${KILL_CLUSTER_MARKER}" |
| export YARN_OPTS+=" -D${KILL_CLUSTER_MARKER}" |
| |
| # This is for KMS. |
| export CATALINA_OPTS+=" -D${KILL_CLUSTER_MARKER}" |
| |
| : ${SERVICE_NAME=$(basename "$0")} |
| PID_FILE="$PID_DIR/$SERVICE_NAME" |
| |
| function do_start { |
| local CMD="$1" |
| shift |
| LOG_FILE="$LOG_DIR/$SERVICE_NAME.out" |
| "$CMD" "$@" &> "$LOG_FILE" & |
| local PID=$! |
| echo $PID > "$PID_FILE" |
| disown -h $PID |
| # Give the process some time to die... if thing go wrong it usually takes at least a |
| # few seconds for the process to totallly shutdown |
| sleep 5 |
| if ! pid_exists $PID; then |
| echo "Failed to start $SERVICE_NAME. The end of the log ($LOG_FILE) is:" |
| tail -20 "$LOG_FILE" |
| return 1 |
| fi |
| } |
| |
| function read_pid { |
| if [[ -e "$PID_FILE" ]]; then |
| cat "$PID_FILE" |
| fi |
| } |
| |
| function pid_exists { |
| if [[ $# -ne 1 ]]; then |
| echo pid_exists must be called with a single argument -- the pid to check 1>&2 |
| exit 1 |
| fi |
| kill -0 $1 &> /dev/null |
| } |
| |
| function stop { |
| local PID=$(read_pid) |
| if [[ -z $PID ]]; then |
| echo No PID to stop |
| return |
| fi |
| if pid_exists $PID; then |
| kill $PID |
| fi |
| |
| # Wait up to 120 seconds for the service to stop. |
| local TIMEOUT_SECS=120 |
| local DEADLINE=$(($(date +%s) + $TIMEOUT_SECS)) |
| while [[ $(date +%s) -lt $DEADLINE ]]; do |
| if ! pid_exists $PID; then |
| # Remove the pid file so that there is no confusion if the pids wrap |
| rm -f $PID_FILE |
| return |
| fi |
| sleep 0.1 |
| done |
| echo "$SERVICE_NAME did not stop after $TIMEOUT_SECS seconds" 1>&2 |
| return 1 |
| } |
| |
| function restart { |
| if status &>/dev/null; then |
| stop |
| fi |
| start |
| } |
| |
| function status { |
| local PID=$(read_pid) |
| if [[ -z $PID ]]; then |
| echo Not started |
| return 1 |
| fi |
| |
| if pid_exists $PID; then |
| echo Running |
| else |
| echo Not Running |
| return 1 |
| fi |
| } |