Skip to content

Commit fbb9e48

Browse files
committed
perf[deploy]: simple deploy.sh
1 parent 761fb4e commit fbb9e48

1 file changed

Lines changed: 36 additions & 104 deletions

File tree

deploy.sh

Lines changed: 36 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,46 @@
1-
#!/bin/bash
1+
#!/bin/sh
22

3-
# Ubuntu需要将dash取消才能运行shell脚本,sudo dpkg-reconfigure dash
3+
# To run shell scripts on Ubuntu, you need to disable dash,sudo dpkg-reconfigure dash
44

55
# doc:
6-
# EN:Server deployment script, this script encapsulates common deployment operations, not necessarily used
7-
# CN:服务器部署脚本,这个脚本封装了常用部署操作,不是一定要使用的
6+
# Server deployment script, this script encapsulates common deployment operations, not necessarily used
87

9-
# EN:The deploy.sh script includes, start the server, gracefully stop the server, update the program file, restart the server, corresponding to four commands
10-
# CN:deploy.sh脚本包括了,启动服务器,优雅停止服务器,更新程序文件,重新启动服务器,对应四个命令:start|stop|update|stopUpdateStart。
8+
# The deploy.sh script includes, start the server, gracefully stop the server, update the program file, restart the server, corresponding to four commands
119

1210
##############################################################################################################################################################################################################################################################################
1311
# usage 1:
1412
# sh deploy.sh start /usr/local/tank/single-boot/single-boot-1.0.jar
1513

16-
# EN:start starts the server, the absolute path is very important,[/usr/local/tank/single-boot/single-boot-1.0.jar] is the absolute path of the jar package,
17-
18-
# CN:start启动服务器,绝对路径很重要,[/usr/local/tank/single-boot/single-boot-1.0.jar]是jar包的绝对路径
14+
# start starts the server, the absolute path is very important,[/usr/local/tank/single-boot/single-boot-1.0.jar] is the absolute path of the jar package,
1915

2016
####################################################################################################################################################################################
2117
# usage 2:
2218
# sh deploy.sh stop single
2319

24-
# EN:stop closes the server, use command jps | grep single,grab the java process that needs to be closed, so you don't need to enter the full name of single-boot
20+
# stop closes the server, use command jps | grep single,grab the java process that needs to be closed, so you don't need to enter the full name of single-boot
2521
# If you have two launchers named [single-boot1.jar], [single-boot2], both will be turned off
2622

27-
# CN:stop关闭服务器,会使用jps | grep single,抓取需要关闭的java进程,所以不需要输入single-boot的全名(不是绝对路径)
28-
# 如果你有两个名称为[single-boot1.jar],[single-boot2]的启动程序,则会把这两个都关闭
29-
3023
####################################################################################################################################################################################
3124
# usage 3:
3225
# sh deploy.sh update /usr/local/tank/single-boot/single-boot-1.0.jar
3326

34-
# EN:update updates the jar package. By default, the jar package under the root path [single-boot-1.0.jar] will be
27+
# update updates the jar package. By default, the jar package under the root path [single-boot-1.0.jar] will be
3528
# used to update the jar package of [/usr/local/tank/single-boot/single-boot-1.0.jar]
3629
# In this way, the jar must be placed in the root directory (the topmost directory, not the root directory, don't make a mistake),
3730
# it is very important, because the jar of the root path is used by default to update the jar at the location you specify
3831

39-
# CN:update更新jar包,默认会使用根路径[/single-boot-1.0.jar]下的jar包去更新[/usr/local/tank/single-boot/single-boot-1.0.jar]的jar包
40-
# 此种方式jar必须放在 / 根目录(最上层的目录,不是root目录,不要弄错了),非常重要,因为默认使用根路径的jar取更新你指定位置的jar
41-
4232
####################################################################################################################################################################################
4333
# usage 4:
4434
# sh deploy.sh stopUpdateStart /usr/local/tank/single-boot/single-boot-1.0.jar
4535

46-
# EN:stop Update Start, execute the commands stop, update, start, three-in-one commands in order,
36+
# stop Update Start, execute the commands stop, update, start, three-in-one commands in order,
4737
# because there is an update, so you must put your jar in the root directory
4838

49-
# CN:stopUpdateStart,按顺序执行命令stop,update,start,三合一的命令,因为有个update,所以必须把你的jar放在根目录(非常重要)
50-
# stop的是[single-boot-1.0.jar],会分割字符串[/usr/local/tank/single-boot/single-boot-1.0.jar]获取到最后的分号后的名称
51-
# update的是[/usr/local/tank/single-boot/single-boot-1.0.jar],所以必须把jar放在 / 根目录(最上层的目录,不是root目录,不要弄错了),非常重要
52-
# start同[usage 1]
53-
5439
####################################################################################################################################################################################
55-
# @author godotg
5640

5741
## java config
5842
JAVA_HOME="/usr/local/java"
59-
JAVA_JVM_OPTIONS="-Dspring.profiles.active=pro -XX:InitialHeapSize=1g -XX:MaxHeapSize=1g -XX:AutoBoxCacheMax=20000 -XX:+UseStringDeduplication -XX:+HeapDumpOnOutOfMemoryError -Djdk.attach.allowAttachSelf=true -Duser.timezone=Asia/Shanghai -Dfile.encoding=UTF-8"
43+
JAVA_JVM_OPTIONS="-Dspring.profiles.active=pro -XX:InitialHeapSize=1g -XX:MaxHeapSize=1g -XX:+AlwaysPreTouch -XX:AutoBoxCacheMax=10000 -XX:+UseStringDeduplication -XX:+HeapDumpOnOutOfMemoryError -Djdk.attach.allowAttachSelf=true -Duser.timezone=Asia/Shanghai -Dfile.encoding=UTF-8"
6044
## log config
6145
WAIT_LOG=true
6246
LOG_FILE="spring.log"
@@ -70,10 +54,9 @@ fi
7054

7155
command=${1}
7256

73-
# 为了避免一些部署的异常,每次执行脚本都更新一下环境变量
74-
source /etc/profile
57+
. /etc/profile
7558

76-
function waitAllProcessesExit() {
59+
waitAllProcessesExit() {
7760
while true; do
7861
local runningProcesses
7962
runningProcesses=$(${JAVA_HOME}/bin/jps -lvm | grep ${1})
@@ -84,103 +67,56 @@ function waitAllProcessesExit() {
8467
sleep 3
8568
else
8669
echo "Gracefully shut down ${1} processes"
87-
return
70+
break
8871
fi
8972
done
9073
}
9174

92-
function waitLogFile() {
93-
local logfile
75+
waitLogFile() {
76+
local currentPath=$(pwd)
9477
while true; do
95-
logfile=$(ls ./ | grep ${LOG_FILE})
96-
if [ -z "${logfile}" ]; then
97-
echo "Waiting for ${LOG_FILE} to be created..."
98-
sleep 1
99-
else
100-
return
101-
fi
102-
103-
if [ -z "${logfile}" ]; then
104-
echo "Waiting for ${LOG_FILE} to be created..."
105-
sleep 2
106-
else
107-
return
108-
fi
109-
110-
if [ -z "${logfile}" ]; then
111-
echo "Waiting for ${LOG_FILE} to be created..."
112-
sleep 3
113-
else
114-
return
115-
fi
116-
117-
if [ -z "${logfile}" ]; then
118-
echo "If there is no response for a long time, solution 1: Please check the startup log, or whether there is a generated log file in the running directory"
119-
echo "如果长时间无响应,解决方法1:请检测启动log日志,或者运行目录是否有生成的日志文件"
120-
sleep 1
121-
else
122-
return
123-
fi
124-
125-
if [ -z "${logfile}" ]; then
126-
echo "If there is no response for a long time, solution 2: Confirm whether the jvm parameter -Dspring.profiles.active=pro is correct and whether it is the operating environment configuration you expect"
127-
echo "如果长时间无响应,解决方法2:确认jvm参数-Dspring.profiles.active=pro是否正确,是否是你期望的运行环境配置"
128-
sleep 1
129-
else
130-
return
131-
fi
132-
133-
if [ -z "${logfile}" ]; then
134-
echo "If there is no response for a long time, solution 3: directly use the command java -Dspring.profiles.active=pro -jar xxx.jar"
135-
echo "如果长时间无响应,解决方法3:直接使用命令 java -Dspring.profiles.active=pro -jar xxx.jar"
136-
sleep 1
137-
else
138-
return
139-
fi
140-
78+
local logfile=$(ls ./ | grep ${LOG_FILE})
14179
if [ -z "${logfile}" ]; then
142-
echo "If there is no response for a long time, solution 4: Run the main function in Idea, add the -Dspring.profiles.active=pro parameter, it can run in Idea and it must also run on the server"
143-
echo "如果长时间无响应,解决方法4:在Idea中运行main函数,加上-Dspring.profiles.active=pro参数,在Idea中能够运行也一定能在服务器运行"
80+
echo "Waiting for ${currentPath}/${LOG_FILE} to be created..."
14481
sleep 1
14582
else
146-
return
83+
break
14784
fi
14885
done
14986

15087
tail -f ${LOG_FILE}
15188
}
15289

153-
# 停止所有进程,不包括login
154-
function stop() {
90+
# Stop all processes
91+
stop() {
15592
echo "######################################################################################################################### Ⅰ stop #########################################################################################################################"
15693
local pids
15794
pids=$(${JAVA_HOME}/bin/jps | grep ${1} | awk '{print $1}' | paste -d " " -s)
15895

15996
if [ -z "${pids}" ]; then
160-
echo "Did not find any Java process containing the ${1} keyword"
161-
echo "没有找到任何包含${1}关键字的Java进程"
97+
echo "Can not find any Java process containing the [${1}] keyword"
16298
return
16399
fi
164100

165101
echo "************************************* cpu *************************************"
166102
uptime
167-
echo "uptime查看cpu的负载情况,load avarage分别是1分钟,5分钟,15分钟内系统的load值,一般load值不大于3,我们就认为它的负载是正常的,大于了3就要想办法降低系统的负载"
103+
echo "uptime -> cpu, load avarage represents the system load values within 1 minute, 5 minutes, and 15 minutes."
168104

169105
echo "************************************* memory *************************************"
170-
free -m
171-
echo "free查看内存的使用情况,重点关注swap内存使用,swap大表示物理内存不够用,这时候容易导致OOM异常"
106+
free -h
107+
echo "free -> memory"
172108

173109
echo "************************************* disk *************************************"
174110
df -h
175-
echo "df查看磁盘的使用情况,应该特别关注日志和数据库的挂载路径的使用情况"
111+
echo "df -> disk"
176112

177113
echo "************************************* network *************************************"
178114
sar -n DEV 1 2
179-
echo "sar查看网络的使用情况,可以通过网络设备的吞吐量,判断网络设备是否已经饱和。"
115+
echo "sar -> net io"
180116

181117
echo "************************************* system log *************************************"
182-
dmesg | tail -n 5
183-
echo "df查看系统日志的最后20行,主要看有没有严重的系统问题"
118+
dmesg | tail -n 8
119+
echo "dmesg -> system log"
184120

185121
echo "************************************* jmap *************************************"
186122
for pid in ${pids}; do
@@ -197,7 +133,7 @@ function stop() {
197133
}
198134

199135
# 更新jar包
200-
function update() {
136+
update() {
201137
echo "######################################################################################################################### Ⅱ update #########################################################################################################################"
202138
local jarPath=${1}
203139

@@ -231,9 +167,9 @@ function update() {
231167
echo "${fileInfo}"
232168
}
233169

234-
# 启动一个java进程,必须指定jar路径和jar名称
235-
# -n为notEmpty,-z为empty
236-
function start() {
170+
# To start a Java process, you must specify the path to the JAR file and the JAR file name.
171+
# -n is notEmpty, -z is empty
172+
start() {
237173
echo "######################################################################################################################### Ⅲ start #########################################################################################################################"
238174
local jarPath=${1}
239175

@@ -246,20 +182,16 @@ function start() {
246182
local jarFilePath=${jarPath%/*}
247183
cd ${jarFilePath}
248184

249-
local currentPath
250-
currentPath=$(pwd)
185+
local currentPath=$(pwd)
251186
echo "pwd current path : ${currentPath}"
252187

253188
if [ ! -f "${jarPath}" ]; then
254-
echo "文件不存在:启动的jar包不存在,请检查jar的路径格式是否是绝对路径"
255189
echo "usage: sh deploy.sh start jarAbsPath"
256190
exit 1
257191
fi
258192

259-
# -XX:+AlwaysPreTouch,并置零内存页面,可能令得启动时慢上一点,但后面访问时会更流畅,比如页面会连续分配
260-
# 输出到文件 >> output.log 2>&1 &
261-
echo "${JAVA_HOME}/bin/java ${JAVA_JVM_OPTIONS} -jar ${jarPath} >/dev/null 2>&1 &"
262-
nohup ${JAVA_HOME}/bin/java ${JAVA_JVM_OPTIONS} -jar ${jarPath} >/dev/null 2>&1 &
193+
echo "${JAVA_HOME}/bin/java ${JAVA_JVM_OPTIONS} -jar ${jarPath} &> /dev/null &"
194+
nohup ${JAVA_HOME}/bin/java ${JAVA_JVM_OPTIONS} -jar ${jarPath} &> /dev/null &
263195

264196
# If there is no info log, keep waiting
265197
if $WAIT_LOG; then
@@ -268,7 +200,7 @@ function start() {
268200
}
269201

270202

271-
function stopUpdateStart() {
203+
stopUpdateStart() {
272204
local jarPath=${1}
273205

274206
if [ -z "${jarPath}" ]; then
@@ -294,7 +226,7 @@ function stopUpdateStart() {
294226
}
295227

296228
# Linux性能调优
297-
function optimizeLinux() {
229+
optimizeLinux() {
298230
echo "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
299231
echo "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"
300232
echo "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------"

0 commit comments

Comments
 (0)