core.sh

  1. #!/usr/bin/env bash
  2. BASE="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
  3. cd $BASE
  4. ################################
  5. # params
  6. ################################
  7. #parse comand name, app name, app parameter
  8. OPERATE=$1
  9. shift
  10. MODE=$1
  11. shift
  12. params=$@
  13. ################################
  14. # constants
  15. ################################
  16. #opts
  17. JAVA_OPTS="-Xmx2G -Xms2G"
  18. JAVA_OPTS="${JAVA_OPTS} -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true"
  19. if [ "${MODE}" = "debug" ]; then
  20. JAVA_OPTS="${JAVA_OPTS} -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n "
  21. fi
  22. if [ "${MODE}" = "jmx" ]; then
  23. JAVA_JMX_OPTS=" -Dcom.sun.management.jmxremote.port=1099 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false "
  24. fi
  25. #dir
  26. APP_NAME=api-service
  27. APP_JAR=$BASE/lib/${APP_NAME}-1.0.1.jar
  28. APP_CONF="$BASE/conf"
  29. APP_LOGS="$BASE/logs"
  30. APP_LIB="$BASE/lib"
  31. #file
  32. APP_OUT="${APP_LOGS}/${APP_NAME}.out"
  33. #port
  34. APP_PORT=`cat ${APP_CONF}/application.properties |grep server.port|cut -d '=' -f 2`
  35. ################################
  36. # functions
  37. ################################
  38. # Shell Colors
  39. GREEN=$'\e[0;32m'
  40. LGREEN=$'\e[1;32m'
  41. RED=$'\e[0;31m'
  42. LRED=$'\e[1;31m'
  43. BLUE=$'\e[0;34m'
  44. LBLUE=$'\e[1;34m'
  45. RESET=$'\e[m'
  46. function error() {
  47. debug error $@
  48. }
  49. function debug() {
  50. if [[ "$1" == "warn" ]]; then
  51. shift
  52. echo -e " ${LBLUE}$1${RESET}"
  53. elif [[ "$1" == "info" ]]; then
  54. shift
  55. echo -e " ${BLUE}$1${RESET}"
  56. elif [[ "$1" == "error" ]]; then
  57. shift
  58. echo -e " ${RED}ERROR:${LRED} $@${RESET}"
  59. exit 1
  60. else
  61. echo -e $@
  62. fi
  63. }
  64. function cmd() {
  65. echo -e "CMD: $OPERATE \t $params"
  66. }
  67. function env() {
  68. cat << EOF
  69. ENV:
  70. APP_CONF: ${APP_CONF}
  71. APP_LOGS: ${APP_LOGS}
  72. APP_OUT : ${APP_OUT}
  73. APP_LIB : ${APP_LIB}
  74. EOF
  75. }
  76. function init() {
  77. mkdir -p ${APP_CONF} ${APP_LOGS}
  78. }
  79. function check() {
  80. PID=`ps -ef|grep java|grep "\-jar"|grep ${APP_JAR}|awk '{print $2}'`
  81. if [ -n "$PID" ]; then
  82. debug error "The ${APP_NAME} already started! PID: $PID"
  83. exit 1
  84. fi
  85. }
  86. function start() {
  87. check
  88. CONFIG_FILES=" -Dlogging.path=${APP_LOGS} -Dlogging.config=${APP_CONF}/log4j2.xml -Dspring.config.location=${APP_CONF}/application.properties "
  89. cd $BASE
  90. nohup java $JAVA_OPTS $CONFIG_FILES -jar ${APP_JAR} $params > ${APP_OUT} 2>&1 &
  91. PID=$!
  92. debug info "${APP_NAME}(pid ${PID}) is started."
  93. }
  94. function status() {
  95. PID=`ps -ef|grep java|grep "\-jar"|grep ${APP_JAR}|awk '{print $2}'`
  96. if [ -n "$PID" ]; then
  97. debug info "${APP_NAME}(pid ${PID}) is running..."
  98. else
  99. debug info "${APP_NAME} is not running."
  100. fi
  101. }
  102. function stop() {
  103. PID=`ps -ef|grep java|grep "\-jar"|grep ${APP_JAR}|awk '{print $2}'`
  104. if [ -n "$PID" ]; then
  105. kill ${PID}
  106. debug info "${APP_NAME}(pid ${PID}) is stopped"
  107. exit 0
  108. else
  109. debug error "${APP_NAME} is not running."
  110. fi
  111. }
  112. ################################
  113. # run
  114. ################################
  115. init
  116. cmd
  117. env
  118. echo "RES:"
  119. case $OPERATE in
  120. start)
  121. start
  122. ;;
  123. stop)
  124. stop
  125. ;;
  126. status)
  127. status
  128. ;;
  129. esac
  1. #!/bin/sh -
  2. BASE="$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )"
  3. $BASE/bin/core.sh start
文档更新时间: 2019-06-21 20:02