startJar.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. APP_NAME=
  2. APP_DEBUG=
  3. date=`date`
  4. usage() {
  5. echo "case: sh run.sh [start|stop|restart|status|checkServer]"
  6. echo "请类似这样执行 ./*.sh start prod or ./*sh restart prod"
  7. exit 1
  8. }
  9. checkEnv(){
  10. if [ -z "${APP_NAME}" ] || [ -z "${APP_DEBUG}" ]; then #判断pid是否为空
  11. if [[ $1 = 'prod' ]]; then
  12. echo "prod config"
  13. APP_NAME=fdkk-meta-prod.jar
  14. APP_DEBUG=5527
  15. elif [[ $1 = 'uat' ]] ; then
  16. echo "uat config"
  17. APP_NAME=fdkk-meta-uat.jar
  18. APP_DEBUG=5010
  19. fi
  20. fi
  21. }
  22. # 判断当前服务是否已经启动的函数
  23. is_exist(){
  24. checkEnv $1
  25. pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` #根据ps 命令查询当前服务的进程号 赋值给pid"
  26. if [ -z "${pid}" ]; then #判断pid是否为空
  27. echo "pid 不存在"
  28. return 1
  29. else
  30. echo "pid 存在"
  31. return 0
  32. fi
  33. }
  34. start(){
  35. checkEnv $1
  36. is_exist
  37. if [ $? -eq "0" ]; then # [$? -eq "0"] 说明pid不等于空 说明服务正在运行中,将进程号打印出来
  38. echo "${APP_NAME} running. pid=${pid}"
  39. else
  40. nohup java -jar --spring.profiles.active=$1 -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${APP_DEBUG} $APP_NAME --spring.profiles.active=$1 > logs.log 2>error.log & # 说明pid为空
  41. # 执行java -jar 命令启动服务
  42. echo "${APP_NAME} started启动"
  43. fi
  44. }
  45. stop(){
  46. echo "执行 stop 方法"
  47. checkEnv $1
  48. is_exist
  49. if [ $? -eq "0" ]; then # [$? -eq "0"] 说明pid不等于空 说明服务正在运行中,将进程号杀死
  50. kill -9 $pid
  51. echo "${pid} stopped-停止"
  52. else
  53. echo "${APP_NAME} 没有运行"
  54. fi
  55. }
  56. status(){
  57. echo "执行 status 方法"
  58. checkEnv $1
  59. is_exist
  60. if [ $? -eq "0" ]; then
  61. echo "${APP_NAME} running-启动. Pid is ${pid}"
  62. else
  63. echo "${APP_NAME} 没有运行"
  64. fi
  65. }
  66. checkServer(){
  67. echo "执行 status 方法"
  68. checkEnv $1
  69. is_exist
  70. if [ $? -eq "0" ]; then
  71. echo "${APP_NAME} running-正在运行. Pid is ${pid}"
  72. echo `date +%Y-%m-%d` `date +%H:%M:%S` ${APP_NAME} "running-正在运行. Pid is ${pid}" >> check.log
  73. exit 0
  74. else
  75. echo "${APP_NAME} 没有运行,即将重启"
  76. echo `date +%Y-%m-%d` `date +%H:%M:%S` ${APP_NAME} "没有运行,即将重启" >> check.log
  77. restart $1
  78. fi
  79. }
  80. # 重启命令其实就是先执行关闭命令 再执行重启命令
  81. restart(){
  82. echo "执行 restart 方法"
  83. checkEnv $1
  84. is_exist
  85. if [ $? -eq "0" ]; then # [$? -eq "0"] 说明pid不等于空 说明服务正在运行中,将进程号杀死
  86. kill -9 $pid
  87. echo "${pid} stopped-停止"
  88. else
  89. echo "${APP_NAME} 没有运行"
  90. fi
  91. sleep 5
  92. is_exist
  93. if [ $? -eq "0" ]; then # [$? -eq "0"] 说明pid不等于空 说明服务正在运行中,将进程号打印出来
  94. echo "${APP_NAME} running. pid=${pid}"
  95. else
  96. nohup java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=${APP_DEBUG} $APP_NAME --spring.profiles.active=$1 > logs.log 2>error.log & # 说明pid为空 执行java -jar 命令启动服务
  97. echo "${APP_NAME} started启动"
  98. fi
  99. }
  100. # 这里的$1 取的是当前输入命令 的第二个参数 ./start.sh start
  101. case "$1" in
  102. "start")
  103. start $2
  104. ;;
  105. "stop")
  106. stop $2
  107. ;;
  108. "status")
  109. status $2
  110. ;;
  111. "restart")
  112. restart $2
  113. ;;
  114. "checkServer")
  115. checkServer $2
  116. ;;
  117. *)
  118. usage
  119. ;;
  120. esac