|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# ------------------------------------------------------------------------------ |
| 4 | +# Java 应用运维脚本 |
| 5 | +# @author Zhang Peng |
| 6 | +# ------------------------------------------------------------------------------ |
| 7 | + |
| 8 | +# ------------------------------------------------------------------------------ env preparation |
| 9 | +# load libs |
| 10 | +CURRENT_PATH=`dirname ${BASH_SOURCE[0]}` |
| 11 | +source ${CURRENT_PATH}/env.sh |
| 12 | + |
| 13 | +# ------------------------------------------------------------------------------ functions |
| 14 | + |
| 15 | +stopServer() { |
| 16 | + if [[ ! $1 ]]; then |
| 17 | + printError "please input java app name" |
| 18 | + return ${FAILED} |
| 19 | + fi |
| 20 | + |
| 21 | + local javaAppName=$1 |
| 22 | + local pid=`jps | grep ${javaAppName} | awk '{print $1}'` |
| 23 | + if [[ -n "${pid}" ]]; then |
| 24 | + kill -9 ${pid} |
| 25 | + if [[ $? -eq ${SUCCEED} ]]; then |
| 26 | + printInfo "stop ${javaAppName} succeed" |
| 27 | + return ${SUCCEED} |
| 28 | + else |
| 29 | + printError "stop ${javaAppName} failed" |
| 30 | + return ${FAILED} |
| 31 | + fi |
| 32 | + else |
| 33 | + printWarn "${javaAppName} is not running" |
| 34 | + return ${SUCCEED} |
| 35 | + fi |
| 36 | +} |
| 37 | + |
| 38 | +startServer() { |
| 39 | + if [[ ! $1 ]]; then |
| 40 | + printError "please input java app name" |
| 41 | + return ${FAILED} |
| 42 | + fi |
| 43 | + |
| 44 | + # >>>> 1. check java app is started or not |
| 45 | + # >>>> 1.1. exit script if the app is started |
| 46 | + local javaAppName=$1 |
| 47 | + local pid=`jps | grep ${javaAppName} | awk '{print $1}'` |
| 48 | + if [[ -n "${pid}" ]]; then |
| 49 | + printInfo "${javaAppName} is started, PID: ${pid}" |
| 50 | + return ${SUCCEED} |
| 51 | + fi |
| 52 | + |
| 53 | + # >>>> 2. package options |
| 54 | + # GC OPTS |
| 55 | + local javaOptions="-server -Xms1g -Xmx2g -Xss256k" |
| 56 | + javaOptions="${javaOptions} -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:NewRatio=4" |
| 57 | + |
| 58 | + # GC LOG OPTS |
| 59 | + javaOptions="${javaOptions} -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:+PrintGCDateStamps" |
| 60 | + javaOptions="${javaOptions} -verbose:gc -Xloggc:${LOG_PATH}/${javaAppName}.gc.log" |
| 61 | + javaOptions="${javaOptions} -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M" |
| 62 | + |
| 63 | + # Heap Dump OPTS |
| 64 | + javaOptions="${javaOptions} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError" |
| 65 | + javaOptions="${javaOptions} -XX:HeapDumpPath=${LOG_PATH}/${javaAppName}.heapdump.hprof" |
| 66 | + |
| 67 | + # APP OPTS |
| 68 | + javaOptions="${javaOptions} -Dsun.net.inetaddr.ttl=60 -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8" |
| 69 | + if [[ ${PROFILE} ]]; then |
| 70 | + javaOptions="${javaOptions} -Dspring.profiles.active=${PROFILE}" |
| 71 | + fi |
| 72 | + |
| 73 | + # DEBUG OPTS |
| 74 | + if [[ "${DEBUG}" == "on" ]]; then |
| 75 | + # JMX OPTS |
| 76 | + local ip=$(ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d '/') |
| 77 | + local jmxPort=$(expr 10000 + ${PORT}) |
| 78 | + javaOptions="${javaOptions} -Dcom.sun.management.jmxremote=true" |
| 79 | + javaOptions="${javaOptions} -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false" |
| 80 | + javaOptions="${javaOptions} -Djava.rmi.server.hostname=${ip} -Dcom.sun.management.jmxremote.port=${jmxPort}" |
| 81 | + |
| 82 | + # Remote Debug |
| 83 | + local debugPort=$(expr 20000 + ${PORT}) |
| 84 | + javaOptions="${javaOptions} -Xdebug -Xnoagent -Djava.compiler=NONE" |
| 85 | + javaOptions="${javaOptions} -Xrunjdwp:transport=dt_socket,address=${debugPort},server=y,suspend=n" |
| 86 | + fi |
| 87 | + |
| 88 | + # CLASSPATH |
| 89 | + local appOptions="-classpath ${ROOT_PATH}/lib/* -Dlogging.config=file:${ROOT_PATH}/config/logback.dev.xml" |
| 90 | + appOptions="${appOptions} --spring.config.location=classpath:/,classpath:/config/,file:${ROOT_PATH},file:${ROOT_PATH}/config/" |
| 91 | + if [[ ${PORT} ]]; then |
| 92 | + appOptions="${appOptions} --server.port=${PORT}" |
| 93 | + fi |
| 94 | + |
| 95 | + # >>>> 3. create log dir and console log file |
| 96 | + mkdir -p ${LOG_PATH} |
| 97 | + if [[ ! -f ${CONSOLE_LOG} ]]; then |
| 98 | + touch ${CONSOLE_LOG} |
| 99 | + fi |
| 100 | + |
| 101 | + # >>>> 4. start java app |
| 102 | + printInfo "starting ${javaAppName}, execute cli: " |
| 103 | + printInfo "nohup java ${javaOptions} -jar ${ROOT_PATH}/${javaAppName}.jar ${appOptions} >> ${CONSOLE_LOG} 2>&1 &" |
| 104 | + nohup java ${javaOptions} -jar ${ROOT_PATH}/${javaAppName}.jar ${appOptions} >> ${CONSOLE_LOG} 2>&1 & |
| 105 | + |
| 106 | + # >>>> 5. check java app is started or not |
| 107 | + local pid=`jps | grep ${javaAppName} | awk '{print $1}'` |
| 108 | + if [[ -n "${pid}" ]]; then |
| 109 | + printInfo "start ${javaAppName} succeed, PID: ${pid}" |
| 110 | + return ${SUCCEED} |
| 111 | + else |
| 112 | + printError "start ${javaAppName} failed" |
| 113 | + return ${FAILED} |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +# ------------------------------------------------------------------------------ main |
| 118 | +export LANG="zh_CN.UTF-8" |
| 119 | +ROOT_PATH=$(cd ${CURRENT_PATH}/..; pwd) |
| 120 | + |
| 121 | +APP_NAME=java-app |
| 122 | +LOG_PATH=/var/log/myapp |
| 123 | +CONSOLE_LOG=${LOG_PATH}/${APP_NAME}.console.log |
| 124 | + |
| 125 | +PORT=8888 |
| 126 | +PROFILE=dev |
| 127 | +DEBUG=off |
| 128 | + |
| 129 | +startServer ${APP_NAME} |
| 130 | +#stopServer ${APP_NAME} |
| 131 | +if [[ $? -eq ${SUCCEED} ]]; then |
| 132 | + exit ${SUCCEED} |
| 133 | +else |
| 134 | + exit ${FAILED} |
| 135 | +fi |
0 commit comments