run.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/bin/sh
  2. S_ACTIVE=dev
  3. JAVA_OPS='-Xms512M -Xmx512M'
  4. CURR_PATH=$(pwd)
  5. SERVICE_NAME=""
  6. JAR_NAME=$(find ./ -maxdepth 1 -name \*1.0.0-SNAPSHOT-exec.jar -type f -exec basename {} \;)
  7. if [ -z $JAR_NAME ];then
  8. JAR_NAME=$(find ./ -maxdepth 1 -name \*1.0.0-SNAPSHOT.jar -type f -exec basename {} \;)
  9. if [ -z $JAR_NAME ];then
  10. JAR_NAME=$(find ./ -maxdepth 1 -name \*1.0.0-RELEASE-exec.jar -type f -exec basename {} \;)
  11. SERVICE_NAME=${JAR_NAME%%-1.0.0-RELEASE-exec.jar}
  12. else
  13. SERVICE_NAME=${JAR_NAME%%-1.0.0-SNAPSHOT.jar}
  14. fi
  15. else
  16. SERVICE_NAME=${JAR_NAME%%-1.0.0-SNAPSHOT-exec.jar}
  17. fi
  18. #create tmp dir
  19. TMP_DIR=${CURR_PATH}"/tmp"
  20. mkdir -p $TMP_DIR
  21. JAVA_OPS=" -Dspring.profiles.active=$S_ACTIVE -Dtmp.dir=${TMP_DIR} "${JAVA_OPS}
  22. JAR_LIB=${CURR_PATH}"/lib"
  23. if [ -d $JAR_LIB ];then
  24. JAVA_OPS=" -Dloader.path=file://${JAR_LIB} "${JAVA_OPS}
  25. fi
  26. PATH_TO_JAR="$CURR_PATH/$JAR_NAME"
  27. PID_PATH_NAME="$CURR_PATH/$SERVICE_NAME-pid"
  28. if [ -f $PID_PATH_NAME ]; then
  29. if test -z $(cat $PID_PATH_NAME) ;then
  30. rm $PID_PATH_NAME
  31. fi
  32. fi
  33. start()
  34. {
  35. echo "Starting $SERVICE_NAME ..."
  36. if [ ! -f $PID_PATH_NAME ]; then
  37. if [ $1 -eq 0 ]; then
  38. java $JAVA_OPS -jar $PATH_TO_JAR
  39. echo "$SERVICE_NAME stoped ..."
  40. else
  41. nohup java $JAVA_OPS -jar $PATH_TO_JAR >/dev/null 2>&1 &
  42. echo $! > $PID_PATH_NAME
  43. echo "$SERVICE_NAME started ..."
  44. fi
  45. else
  46. PID=$(cat $PID_PATH_NAME);
  47. PID_EXISTS=$(ps -p $PID);
  48. if [ $? -eq 0 ]; then
  49. echo "$SERVICE_NAME is already running ..."
  50. else
  51. if [ $1 -eq 0 ]; then
  52. java $JAVA_OPS -jar $PATH_TO_JAR
  53. echo "$SERVICE_NAME stoped ..."
  54. else
  55. nohup java $JAVA_OPS -jar $PATH_TO_JAR >/dev/null 2>&1 &
  56. echo $! > $PID_PATH_NAME
  57. echo "$SERVICE_NAME started ..."
  58. fi
  59. fi
  60. fi
  61. }
  62. stop()
  63. {
  64. if [ -f $PID_PATH_NAME ]; then
  65. PID=$(cat $PID_PATH_NAME);
  66. echo "$SERVICE_NAME stoping ..."
  67. i=0
  68. kill $PID
  69. PID_EXISTS=$(ps -p $PID);
  70. while ( [ $? -eq 0 ] && [ "$i" -lt 15 ] )
  71. do
  72. sleep 1;
  73. let "i += 1";
  74. echo "$i sec"
  75. PID_EXISTS=$(ps -p $PID);
  76. done
  77. PID_EXISTS=$(ps -p $PID);
  78. if [ $? -eq 0 ]; then
  79. CPID=$(ps -ef|grep java|grep $PATH_TO_JAR|grep -v grep|awk '{print $2}')
  80. kill -9 $CPID;
  81. echo "$SERVICE_NAME stop failure so kill process ..."
  82. fi
  83. echo "$SERVICE_NAME stopped ..."
  84. rm $PID_PATH_NAME
  85. else
  86. echo "$SERVICE_NAME is not running ..."
  87. fi
  88. }
  89. case $1 in
  90. start)
  91. start 1
  92. ;;
  93. console)
  94. start 0
  95. ;;
  96. stop)
  97. stop
  98. ;;
  99. restart)
  100. stop
  101. start 1
  102. ;;
  103. *)
  104. echo "Usage: $0 {start|console|stop|restart}"
  105. exit 1
  106. ;;
  107. esac
  108. exit 0