|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Licensed to Apache Software Foundation (ASF) under one or more contributor |
| 4 | +# license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright |
| 6 | +# ownership. Apache Software Foundation (ASF) licenses this file to you under |
| 7 | +# the Apache License, Version 2.0 (the "License"); you may |
| 8 | +# not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, |
| 14 | +# software distributed under the License is distributed on an |
| 15 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | +# KIND, either express or implied. See the License for the |
| 17 | +# specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | + |
| 20 | +# Detect operating system |
| 21 | +OS=$(uname) |
| 22 | + |
| 23 | +EVENTMESH_ADMIN_HOME=`cd $(dirname $0)/.. && pwd` |
| 24 | + |
| 25 | +export EVENTMESH_ADMIN_HOME |
| 26 | + |
| 27 | +function get_pid { |
| 28 | + local ppid="" |
| 29 | + if [ -f ${EVENTMESH_ADMIN_HOME}/bin/pid-admin.file ]; then |
| 30 | + ppid=$(cat ${EVENTMESH_ADMIN_HOME}/bin/pid-admin.file) |
| 31 | + # If the process does not exist, it indicates that the previous process terminated abnormally. |
| 32 | + if [ ! -d /proc/$ppid ]; then |
| 33 | + # Remove the residual file and return an error status. |
| 34 | + rm ${EVENTMESH_ADMIN_HOME}/bin/pid-admin.file |
| 35 | + echo -e "ERROR\t EventMesh admin process had already terminated unexpectedly before, please check log output." |
| 36 | + ppid="" |
| 37 | + fi |
| 38 | + else |
| 39 | + if [[ $OS =~ Msys ]]; then |
| 40 | + # There is a Bug on Msys that may not be able to kill the identified process |
| 41 | + ppid=`jps -v | grep -i "org.apache.eventmesh.admin.server.ExampleAdminServer" | grep java | grep -v grep | awk -F ' ' {'print $1'}` |
| 42 | + elif [[ $OS =~ Darwin ]]; then |
| 43 | + # Known problem: grep Java may not be able to accurately identify Java processes |
| 44 | + ppid=$(/bin/ps -o user,pid,command | grep "java" | grep -i "org.apache.eventmesh.admin.server.ExampleAdminServer" | grep -Ev "^root" |awk -F ' ' {'print $2'}) |
| 45 | + else |
| 46 | + # It is required to identify the process as accurately as possible on Linux |
| 47 | + ppid=$(ps -C java -o user,pid,command --cols 99999 | grep -w $EVENTMESH_ADMIN_HOME | grep -i "org.apache.eventmesh.admin.server.ExampleAdminServer" | grep -Ev "^root" |awk -F ' ' {'print $2'}) |
| 48 | + fi |
| 49 | + fi |
| 50 | + echo "$ppid"; |
| 51 | +} |
| 52 | + |
| 53 | +pid=$(get_pid) |
| 54 | +if [[ $pid == "ERROR"* ]]; then |
| 55 | + echo -e "${pid}" |
| 56 | + exit 9 |
| 57 | +fi |
| 58 | +if [ -z "$pid" ];then |
| 59 | + echo -e "ERROR\t No EventMesh admin server running." |
| 60 | + exit 9 |
| 61 | +fi |
| 62 | + |
| 63 | +kill ${pid} |
| 64 | +echo "Send shutdown request to EventMesh admin(${pid}) OK" |
| 65 | + |
| 66 | +[[ $OS =~ Msys ]] && PS_PARAM=" -W " |
| 67 | +stop_timeout=60 |
| 68 | +for no in $(seq 1 $stop_timeout); do |
| 69 | + if ps $PS_PARAM -p "$pid" 2>&1 > /dev/null; then |
| 70 | + if [ $no -lt $stop_timeout ]; then |
| 71 | + echo "[$no] server shutting down ..." |
| 72 | + sleep 1 |
| 73 | + continue |
| 74 | + fi |
| 75 | + |
| 76 | + echo "shutdown server timeout, kill process: $pid" |
| 77 | + kill -9 $pid; sleep 1; break; |
| 78 | + echo "`date +'%Y-%m-%-d %H:%M:%S'` , pid : [$pid] , error message : abnormal shutdown which can not be closed within 60s" > ../logs/shutdown.error |
| 79 | + else |
| 80 | + echo "shutdown server ok!"; break; |
| 81 | + fi |
| 82 | +done |
| 83 | + |
| 84 | +if [ -f "pid-admin.file" ]; then |
| 85 | + rm pid-admin.file |
| 86 | +fi |
| 87 | + |
| 88 | + |
0 commit comments