run-docker.sh 2.7 KB

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