Launch.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/sh
  2. CWD=$(cd $(dirname $0); pwd)
  3. API_NAME=<API_NAME>
  4. JAR_NAME=$CWD/app\.jar
  5. #PID 代表是PID文件
  6. PID=$CWD/$API_NAME\.pid
  7. SKYWALKING_JAVA_AGENT=-javaagent:/opt/skywalking/agent/skywalking-agent.jar
  8. #使用说明,用来提示输入参数
  9. usage() {
  10. echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
  11. exit 1
  12. }
  13. #检查程序是否在运行
  14. is_exist(){
  15. pid=`ps -ef|grep $JAR_NAME|grep -v grep|awk '{print $2}' `
  16. #如果不存在返回1,存在返回0
  17. if [ -z "${pid}" ]; then
  18. return 1
  19. else
  20. return 0
  21. fi
  22. }
  23. #启动方法
  24. start(){
  25. is_exist
  26. if [ $? -eq "0" ]; then
  27. echo ">>> ${JAR_NAME} is already running PID=${pid} <<<"
  28. else
  29. echo $SKYWALKING_JAVA_AGENT
  30. java_agent=SKYWALKING_JAVA_AGENT
  31. if [[ $SKYWALKING_JAVA_AGENT ]]; then
  32. echo 'Success'
  33. java_agent="$SKYWALKING_JAVA_AGENT -Dskywalking.agent.service_name=$API_NAME"
  34. fi
  35. java_agent = "$java_agent -Dskywalking.agent.service_name=$API_NAME"
  36. echo $java_agent
  37. nohup java <JAVA_OPTS> -jar $JAR_NAME >/dev/null 2>&1 &
  38. echo $! > $PID
  39. echo ">>> start $JAR_NAME successed PID=$! <<<"
  40. fi
  41. }
  42. #停止方法
  43. stop(){
  44. #is_exist
  45. pidf=$(cat $PID)
  46. #echo "$pidf"
  47. echo ">>> api PID = $pidf begin kill $pidf <<<"
  48. kill $pidf
  49. rm -rf $PID
  50. sleep 2
  51. is_exist
  52. if [ $? -eq "0" ]; then
  53. echo ">>> api 2 PID = $pid begin kill -9 $pid <<<"
  54. kill -9 $pid
  55. sleep 2
  56. echo ">>> $JAR_NAME process stopped <<<"
  57. else
  58. echo ">>> ${JAR_NAME} is not running <<<"
  59. fi
  60. }
  61. #输出运行状态
  62. status(){
  63. is_exist
  64. if [ $? -eq "0" ]; then
  65. echo ">>> ${JAR_NAME} is running PID is ${pid} <<<"
  66. else
  67. echo ">>> ${JAR_NAME} is not running <<<"
  68. fi
  69. }
  70. #重启
  71. restart(){
  72. stop
  73. start
  74. }
  75. #根据输入参数,选择执行对应方法,不输入则执行使用说明
  76. case "$1" in
  77. "start")
  78. start
  79. ;;
  80. "stop")
  81. stop
  82. ;;
  83. "status")
  84. status
  85. ;;
  86. "restart")
  87. restart
  88. ;;
  89. *)
  90. usage
  91. ;;
  92. esac
  93. exit 0