|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ----------------------------------------------------------------------------------------------------- |
| 4 | +# 应用终止脚本 |
| 5 | +# @author Zhang Peng |
| 6 | +# ----------------------------------------------------------------------------------------------------- |
| 7 | + |
| 8 | + |
| 9 | +# ------------------------------------------------------------------------------ libs |
| 10 | + |
| 11 | +SCRIPTS_DIR=$(cd `dirname $0`; pwd) |
| 12 | + |
| 13 | +if [[ ! -x ${SCRIPTS_DIR}/utils.sh ]]; then |
| 14 | + logError "${SCRIPTS_DIR}/utils.sh not exists!" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | +source ${SCRIPTS_DIR}/utils.sh |
| 18 | + |
| 19 | + |
| 20 | +# ------------------------------------------------------------------------------ functions |
| 21 | + |
| 22 | +stopServer() { |
| 23 | + if [[ ! $1 ]]; then |
| 24 | + logError "please input java app name" |
| 25 | + return ${ENV_FAILED} |
| 26 | + fi |
| 27 | + |
| 28 | + local appName=$1 |
| 29 | + local pid=`jps | grep ${appName} | awk '{print $1}'` |
| 30 | + if [[ -n "${pid}" ]]; then |
| 31 | + kill -9 ${pid} |
| 32 | + if [[ $? -eq ${ENV_SUCCEED} ]]; then |
| 33 | + printInfo "stop ${appName} succeed" |
| 34 | + return ${ENV_SUCCEED} |
| 35 | + else |
| 36 | + logError "stop ${appName} failed" |
| 37 | + return ${ENV_FAILED} |
| 38 | + fi |
| 39 | + else |
| 40 | + printWarn "${appName} is not running" |
| 41 | + return ${ENV_SUCCEED} |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +startServer() { |
| 46 | + |
| 47 | + # >>>> validate params |
| 48 | + if [[ ! $1 ]] || [[ ! $2 ]] || [[ ! $3 ]] || [[ ! $4 ]]; then |
| 49 | + logError "you must input following params in order:" |
| 50 | + echo -e "${ENV_COLOR_B_RED}" |
| 51 | + echo " (1)jarPath" |
| 52 | + echo " (2)libPath" |
| 53 | + echo " (3)confPath" |
| 54 | + echo " (4)logPath" |
| 55 | + echo " (5)appName [optional]" |
| 56 | + echo " (6)port [optional]" |
| 57 | + echo " (7)profile [optional]" |
| 58 | + echo " (8)debug [optional]" |
| 59 | + echo -e "\nEg. startServer /usr/lib/dunwu/app.jar /usr/lib/dunwu/lib /usr/lib/dunwu/conf /var/log/dunwu dunwu 8888 prod off" |
| 60 | + echo -e "${ENV_COLOR_RESET}" |
| 61 | + return ${ENV_FAILED} |
| 62 | + fi |
| 63 | + |
| 64 | + local jarPath=$1 |
| 65 | + local libPath=$2 |
| 66 | + local confPath=$3 |
| 67 | + local logPath=$4 |
| 68 | + local appName=${5:-myapp} |
| 69 | + local port=${6:-8888} |
| 70 | + local profile=${7:-prod} |
| 71 | + local debug=${8:-off} |
| 72 | + |
| 73 | + # >>>> 1. check java app is started or not |
| 74 | + # >>>> 1.1. exit script if the app is started |
| 75 | + local pid=`jps | grep ${appName} | awk '{print $1}'` |
| 76 | + if [[ -n "${pid}" ]]; then |
| 77 | + printInfo "${appName} is started, PID: ${pid}" |
| 78 | + return ${ENV_SUCCEED} |
| 79 | + fi |
| 80 | + |
| 81 | + # >>>> 2. package options |
| 82 | + # GC OPTS |
| 83 | + local javaOptions="-server -Xms1g -Xmx2g -Xss256k" |
| 84 | + javaOptions="${javaOptions} -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:NewRatio=4" |
| 85 | + |
| 86 | + # GC LOG OPTS |
| 87 | + javaOptions="${javaOptions} -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps" |
| 88 | + javaOptions="${javaOptions} -verbose:gc -Xloggc:${logPath}/${appName}.gc.log" |
| 89 | + javaOptions="${javaOptions} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M" |
| 90 | + |
| 91 | + # Heap Dump OPTS |
| 92 | + javaOptions="${javaOptions} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError" |
| 93 | + javaOptions="${javaOptions} -XX:HeapDumpPath=${logPath}/${appName}.heapdump.hprof" |
| 94 | + |
| 95 | + # APP OPTS |
| 96 | + javaOptions="${javaOptions} -Dsun.net.inetaddr.ttl=60 -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8" |
| 97 | + if [[ ${profile} ]]; then |
| 98 | + javaOptions="${javaOptions} -Dspring.profiles.active=${profile}" |
| 99 | + fi |
| 100 | + |
| 101 | + # DEBUG OPTS |
| 102 | + if [[ "${debug}" == "on" ]]; then |
| 103 | + # JMX OPTS |
| 104 | + local ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d '/') |
| 105 | + local jmxPort=$(expr 10000 + ${port}) |
| 106 | + javaOptions="${javaOptions} -Dcom.sun.management.jmxremote=true" |
| 107 | + javaOptions="${javaOptions} -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false" |
| 108 | + javaOptions="${javaOptions} -Djava.rmi.server.hostname=${ip} -Dcom.sun.management.jmxremote.port=${jmxPort}" |
| 109 | + |
| 110 | + # Remote Debug |
| 111 | + local debugPort=$(expr 20000 + ${port}) |
| 112 | + javaOptions="${javaOptions} -Xdebug -Xnoagent -Djava.compiler=NONE" |
| 113 | + javaOptions="${javaOptions} -Xrunjdwp:transport=dt_socket,address=${debugPort},server=y,suspend=n" |
| 114 | + fi |
| 115 | + |
| 116 | + # CLASSPATH |
| 117 | + local appOptions="-classpath ${libPath}/* -Dlogging.config=file:${confPath}/logback.${profile}.xml" |
| 118 | + local springConfigFiles="classpath:/,classpath:/config/" |
| 119 | + local springConfigFiles="${springConfigFiles},file:${confPath}/,file:${confPath}/application.properties" |
| 120 | + appOptions="${appOptions} --spring.config.location=${springConfigFiles}" |
| 121 | + appOptions="${appOptions} --spring.cache.ehcache.config=file:${confPath}/config/ehcache.xml" |
| 122 | + if [[ ${port} ]]; then |
| 123 | + appOptions="${appOptions} --server.port=${port}" |
| 124 | + fi |
| 125 | + |
| 126 | + # >>>> 3. create log dir and console log file |
| 127 | + local consoleLogPath=${logPath}/${appName}.console.log |
| 128 | + mkdir -p ${logPath} |
| 129 | + if [[ ! -x ${consoleLogPath} ]]; then |
| 130 | + touch ${consoleLogPath} |
| 131 | + fi |
| 132 | + |
| 133 | + # >>>> 4. start java app |
| 134 | + # print bootstrap info |
| 135 | + printInfo "starting ${appName}" |
| 136 | + echo -e "${ENV_COLOR_B_GREEN}" |
| 137 | + echo -e "${ENV_COLOR_B_CYAN}\nBOOT PARAMS:${ENV_COLOR_B_GREEN}\n\n" |
| 138 | + echo "appName=${appName}" |
| 139 | + echo "jarPath=${jarPath}" |
| 140 | + echo "libPath=${libPath}" |
| 141 | + echo "confPath=${confPath}" |
| 142 | + echo "logPath=${logPath}" |
| 143 | + echo "port=${port}" |
| 144 | + echo "profile=${profile}" |
| 145 | + echo "debug=${debug}" |
| 146 | + echo -e "${ENV_COLOR_B_CYAN}\nEXEC CLI:${ENV_COLOR_B_GREEN}\n\n" |
| 147 | + echo "nohup java ${javaOptions} -jar ${jarPath} ${appOptions} >> ${consoleLogPath} 2>&1 &" |
| 148 | + echo -e "${ENV_COLOR_RESET}" |
| 149 | + |
| 150 | + # exec boot cli |
| 151 | + nohup java ${javaOptions} -jar ${jarPath} ${appOptions} >> ${consoleLogPath} 2>&1 & |
| 152 | + |
| 153 | + # >>>> 5. check java app is started or not |
| 154 | + local pid=`jps | grep ${appName} | awk '{print $1}'` |
| 155 | + if [[ -n "${pid}" ]]; then |
| 156 | + printInfo "start ${appName} succeed, PID: ${pid}" |
| 157 | + return ${ENV_SUCCEED} |
| 158 | + else |
| 159 | + logError "start ${appName} failed" |
| 160 | + return ${ENV_FAILED} |
| 161 | + fi |
| 162 | +} |
0 commit comments