stop.sh 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/bin/bash
  2. set -o nounset # 遇到没定义的变量报错后,终止继续执行脚本文件
  3. set -o errexit # 执行失败以后,终止继续执行脚本文件
  4. # Jar包应用名称
  5. STOP_APPLICATION_NAME=$1
  6. # 监听应用停止时间
  7. MAX_TIMEOUT=10
  8. oldPid=$(ps ax | grep $STOP_APPLICATION_NAME | grep java | head -1 | awk '{print $1}')
  9. if [ ${oldPid} ]; then
  10. echo '开始停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】'
  11. kill -15 $oldPid
  12. fi
  13. for((i=1;i<=$MAX_TIMEOUT;i++))
  14. do
  15. # 等待1秒
  16. sleep 1
  17. oldPid=$(ps ax | grep $STOP_APPLICATION_NAME | grep java | head -1 | awk '{print $1}')
  18. if [ ${oldPid} ]; then
  19. echo '正在停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】... ... ('$i's)'
  20. else
  21. break
  22. fi
  23. done
  24. if [ ${oldPid} ]; then
  25. echo '尝试'$MAX_TIMEOUT'次优雅停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】失败,强制停止!!!'
  26. kill -9 $oldPid
  27. else
  28. echo '成功停止【'$STOP_APPLICATION_NAME'】服务, pid【'$oldPid'】'
  29. fi
  30. oldPid=$(ps ax | grep $STOP_APPLICATION_NAME | grep java | head -1 | awk '{print $1}')
  31. if [ ${oldPid} ]; then
  32. echo 'App is running.'
  33. sleep 2
  34. else
  35. echo '【'$STOP_APPLICATION_NAME'】服务停止成功, pid【'$oldPid'】!'
  36. fi
  37. # 删除pid文件
  38. rm -f $STOP_APPLICATION_NAME.pid