service.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. # 待编译的项目
  3. projects=("wutong-okr.jar")
  4. APP_NAME=blade-auth.jar
  5. PROFILES_ACTIVE="$2"
  6. #使用说嘛,提示用户输入参数
  7. usage() {
  8. echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
  9. exit 1
  10. }
  11. start() {
  12. for ((i = 0; i < ${#projects[*]}; i++)); do
  13. APP_NAME=${projects[${i}]}
  14. init
  15. done
  16. }
  17. #监测程序是否在运行
  18. is_exist() {
  19. pid=$(ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}')
  20. #如果不存在返回1,存在返回0
  21. if [ -z "${pid}" ]; then
  22. return 1
  23. else
  24. return 0
  25. fi
  26. }
  27. #启动方法
  28. init() {
  29. echo "${APP_NAME}"
  30. is_exist
  31. if [ $? -eq "0" ]; then
  32. echo "${APP_NAME} is already running. pid=${pid} ."
  33. else
  34. nohup java -Xms1024m -Xmx1024m -jar ${APP_NAME} --spring.profiles.active=${PROFILES_ACTIVE} > ${APP_NAME}_${PROFILES_ACTIVE}.log 2>&1 &
  35. fi
  36. }
  37. #停止方法
  38. stop() {
  39. for ((i = 0; i < ${#projects[*]}; i++)); do
  40. APP_NAME=${projects[${i}]}
  41. is_exist
  42. if [ $? -eq "0" ]; then
  43. kill -9 $pid
  44. else
  45. echo "${APP_NAME} is not running"
  46. fi
  47. done
  48. }
  49. #输出运行状态
  50. status() {
  51. for ((i = 0; i < ${#projects[*]}; i++)); do
  52. APP_NAME=${projects[${i}]}
  53. is_exist
  54. if [ $? -eq "0" ]; then
  55. echo "${APP_NAME} is running. Pid is ${pid}"
  56. else
  57. echo "${APP_NAME} is NOT running."
  58. fi
  59. done
  60. }
  61. #重启
  62. restart() {
  63. stop
  64. start
  65. }
  66. #根据输入参数,选择执行对应方法,不输入则执行使用说明
  67. case "$1" in
  68. "start")
  69. start
  70. ;;
  71. "stop")
  72. stop
  73. ;;
  74. "status")
  75. status
  76. ;;
  77. "restart")
  78. restart
  79. ;;
  80. *)
  81. usage
  82. ;;
  83. esac