run-docker.sh 3.0 KB

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