12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #!/bin/bash
- set -o nounset # 遇到没定义的变量报错后,终止继续执行脚本文件
- set -o errexit # 执行失败以后,终止继续执行脚本文件
- # Jar包应用名称
- STOP_APPLICATION_NAME=$1
- # 监听应用停止时间
- MAX_TIMEOUT=10
- oldPid=$(ps ax | grep $STOP_APPLICATION_NAME | grep java | head -1 | awk '{print $1}')
- if [ ${oldPid} ]; then
- echo '开始停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】'
- kill -15 $oldPid
- fi
- for((i=1;i<=$MAX_TIMEOUT;i++))
- do
- # 等待1秒
- sleep 1
- oldPid=$(ps ax | grep $STOP_APPLICATION_NAME | grep java | head -1 | awk '{print $1}')
- if [ ${oldPid} ]; then
- echo '正在停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】... ... ('$i's)'
- else
- break
- fi
- done
- if [ ${oldPid} ]; then
- echo '尝试'$MAX_TIMEOUT'次优雅停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】失败,强制停止!!!'
- kill -9 $oldPid
- else
- echo '成功停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】'
- fi
- oldPid=$(ps ax | grep $STOP_APPLICATION_NAME | grep java | head -1 | awk '{print $1}')
- if [ ${oldPid} ]; then
- echo 'App is running.'
- sleep 2
- else
- echo '【'$STOP_APPLICATION_NAME'】服务停止成功, pid【'$oldPid'】!'
- fi
- # 删除pid文件
- rm -f $STOP_APPLICATION_NAME.pid
|