Skip to content

Commit 83bf9ef

Browse files
committed
update
1 parent 18d2a58 commit 83bf9ef

5 files changed

Lines changed: 406 additions & 0 deletions

File tree

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
3+
# -----------------------------------------------------------------------------------------------------
4+
# myapp 启动脚本,用于【虚拟机环境】
5+
# @author Zhang Peng
6+
# -----------------------------------------------------------------------------------------------------
7+
8+
9+
# ------------------------------------------------------------------------------ libs
10+
11+
SCRIPTS_DIR=$(dirname ${BASH_SOURCE[0]})
12+
if [[ ! -x ${SCRIPTS_DIR}/lifecycle.sh ]]; then
13+
logError "${SCRIPTS_DIR}/lifecycle.sh not exists!"
14+
exit 1
15+
fi
16+
source ${SCRIPTS_DIR}/lifecycle.sh
17+
18+
19+
# ------------------------------------------------------------------------------ main
20+
21+
APP_DIR=$(cd `dirname $0`/..; pwd)
22+
23+
export LANG="zh_CN.UTF-8"
24+
APP=myapp
25+
JAR_PATH=${APP_DIR}/myapp.jar
26+
LIB_PATH=${APP_DIR}/lib
27+
CONF_PATH=${APP_DIR}/config
28+
LOG_DIR=/var/log/dunwu
29+
PORT=8888
30+
PROFILE=prod
31+
DEBUG=off
32+
33+
declare -a serial
34+
serial=(on off)
35+
echo -n "是否启动 debug 模式(可选值:on|off):"
36+
read DEBUG
37+
if ! echo ${serial[@]} | grep -q ${DEBUG}; then
38+
echo "是否启动 debug 模式(可选值:on|off)"
39+
exit 1
40+
fi
41+
42+
startServer ${JAR_PATH} ${LIB_PATH} ${CONF_PATH} ${LOG_DIR} ${APP} ${PORT} ${PROFILE} ${DEBUG}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
# -----------------------------------------------------------------------------------------------------
4+
# 应用启动脚本
5+
# @author Zhang Peng
6+
# -----------------------------------------------------------------------------------------------------
7+
8+
9+
# ------------------------------------------------------------------------------ libs
10+
11+
SCRIPTS_DIR=$(dirname ${BASH_SOURCE[0]})
12+
if [[ ! -x ${SCRIPTS_DIR}/lifecycle.sh ]]; then
13+
logError "${SCRIPTS_DIR}/lifecycle.sh not exists!"
14+
exit 1
15+
fi
16+
source ${SCRIPTS_DIR}/lifecycle.sh
17+
18+
19+
# ------------------------------------------------------------------------------ main
20+
21+
export LANG="zh_CN.UTF-8"
22+
stopServer myapp
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env bash
2+
3+
# -----------------------------------------------------------------------------------------------------
4+
# Shell Utils
5+
# 使用此脚本,应该先 export ENV_LOG_PATH,指定日志路径;否则将使用默认日志路径 /var/log/shell.log
6+
# @author Zhang Peng
7+
# -----------------------------------------------------------------------------------------------------
8+
9+
# ------------------------------------------------------------------------------ env params
10+
11+
# 颜色状态
12+
# Regular Color
13+
export ENV_COLOR_BLACK="\033[0;30m"
14+
export ENV_COLOR_RED="\033[0;31m"
15+
export ENV_COLOR_GREEN="\033[0;32m"
16+
export ENV_COLOR_YELLOW="\033[0;33m"
17+
export ENV_COLOR_BLUE="\033[0;34m"
18+
export ENV_COLOR_MAGENTA="\033[0;35m"
19+
export ENV_COLOR_CYAN="\033[0;36m"
20+
export ENV_COLOR_WHITE="\033[0;37m"
21+
# Bold Color
22+
export ENV_COLOR_B_BLACK="\033[1;30m"
23+
export ENV_COLOR_B_RED="\033[1;31m"
24+
export ENV_COLOR_B_GREEN="\033[1;32m"
25+
export ENV_COLOR_B_YELLOW="\033[1;33m"
26+
export ENV_COLOR_B_BLUE="\033[1;34m"
27+
export ENV_COLOR_B_MAGENTA="\033[1;35m"
28+
export ENV_COLOR_B_CYAN="\033[1;36m"
29+
export ENV_COLOR_B_WHITE="\033[1;37m"
30+
# Underline Color
31+
export ENV_COLOR_U_BLACK="\033[4;30m"
32+
export ENV_COLOR_U_RED="\033[4;31m"
33+
export ENV_COLOR_U_GREEN="\033[4;32m"
34+
export ENV_COLOR_U_YELLOW="\033[4;33m"
35+
export ENV_COLOR_U_BLUE="\033[4;34m"
36+
export ENV_COLOR_U_MAGENTA="\033[4;35m"
37+
export ENV_COLOR_U_CYAN="\033[4;36m"
38+
export ENV_COLOR_U_WHITE="\033[4;37m"
39+
# Background Color
40+
export ENV_COLOR_BG_BLACK="\033[40m"
41+
export ENV_COLOR_BG_RED="\033[41m"
42+
export ENV_COLOR_BG_GREEN="\033[42m"
43+
export ENV_COLOR_BG_YELLOW="\033[43m"
44+
export ENV_COLOR_BG_BLUE="\033[44m"
45+
export ENV_COLOR_BG_MAGENTA="\033[45m"
46+
export ENV_COLOR_BG_CYAN="\033[46m"
47+
export ENV_COLOR_BG_WHITE="\033[47m"
48+
# Reset Color
49+
export ENV_COLOR_RESET="$(tput sgr0)"
50+
51+
# 常用状态值
52+
export ENV_YES=0
53+
export ENV_NO=1
54+
export ENV_SUCCEED=0
55+
export ENV_FAILED=1
56+
57+
58+
# ------------------------------------------------------------------------------ functions
59+
60+
# 显示打印日志的时间
61+
SHELL_LOG_TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S")
62+
# 那个用户在操作
63+
USER=$(whoami)
64+
# 日志路径
65+
LOG_PATH=${ENV_LOG_PATH:-/var/log/shell.log}
66+
# 日志目录
67+
LOG_DIR=${LOG_PATH%/*}
68+
69+
createLogFileIfNotExists() {
70+
if [[ ! -x "${LOG_PATH}" ]]; then
71+
mkdir -p "${LOG_DIR}"
72+
touch "${LOG_PATH}"
73+
fi
74+
}
75+
76+
logInfo() {
77+
echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}"
78+
createLogFileIfNotExists
79+
echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [INFO] [$0] $@" >> "${LOG_PATH}"
80+
}
81+
82+
logWarn() {
83+
echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}"
84+
createLogFileIfNotExists
85+
echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [WARN] [$0] $@" >> "${LOG_PATH}"
86+
}
87+
88+
logError() {
89+
echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}"
90+
createLogFileIfNotExists
91+
echo "[${SHELL_LOG_TIMESTAMP}] [${USER}] [ERROR] [$0] $@" >> "${LOG_PATH}"
92+
}
93+
94+
printInfo() {
95+
echo -e "${ENV_COLOR_B_GREEN}[INFO] $@${ENV_COLOR_RESET}"
96+
}
97+
98+
printWarn() {
99+
echo -e "${ENV_COLOR_B_YELLOW}[WARN] $@${ENV_COLOR_RESET}"
100+
}
101+
102+
printError() {
103+
echo -e "${ENV_COLOR_B_RED}[ERROR] $@${ENV_COLOR_RESET}"
104+
}
105+
106+
callAndLog () {
107+
$*
108+
if [[ $? -eq ${ENV_SUCCEED} ]]; then
109+
logInfo "$@"
110+
return ${ENV_SUCCEED}
111+
else
112+
logError "$@ EXECUTE ENV_FAILED"
113+
return ${ENV_FAILED}
114+
fi
115+
}

0 commit comments

Comments
 (0)