123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #!/bin/bash
- set -e
- DATE=$(date +%Y%m%d%H%M)
- BASE_PATH=/work/projects/yudao-server
- SOURCE_PATH=$BASE_PATH/build
- SERVER_NAME=yudao-server
- PROFILES_ACTIVE=development
- HEALTH_CHECK_URL=http://127.0.0.1:48080/actuator/health/
- HEAP_ERROR_PATH=$BASE_PATH/heapError
- JAVA_OPS="-Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$HEAP_ERROR_PATH"
- function backup() {
-
- if [ ! -f "$BASE_PATH/$SERVER_NAME.jar" ]; then
- echo "[backup] $BASE_PATH/$SERVER_NAME.jar 不存在,跳过备份"
-
- else
- echo "[backup] 开始备份 $SERVER_NAME ..."
- cp $BASE_PATH/$SERVER_NAME.jar $BASE_PATH/backup/$SERVER_NAME-$DATE.jar
- echo "[backup] 备份 $SERVER_NAME 完成"
- fi
- }
- function transfer() {
- echo "[transfer] 开始转移 $SERVER_NAME.jar"
-
- if [ ! -f "$BASE_PATH/$SERVER_NAME.jar" ]; then
- echo "[transfer] $BASE_PATH/$SERVER_NAME.jar 不存在,跳过删除"
- else
- echo "[transfer] 移除 $BASE_PATH/$SERVER_NAME.jar 完成"
- rm $BASE_PATH/$SERVER_NAME.jar
- fi
-
- echo "[transfer] 从 $SOURCE_PATH 中获取 $SERVER_NAME.jar 并迁移至 $BASE_PATH ...."
- cp $SOURCE_PATH/$SERVER_NAME.jar $BASE_PATH
- echo "[transfer] 转移 $SERVER_NAME.jar 完成"
- }
- function stop() {
- echo "[stop] 开始停止 $BASE_PATH/$SERVER_NAME"
- PID=$(ps -ef | grep $BASE_PATH/$SERVER_NAME | grep -v "grep" | awk '{print $2}')
-
- if [ -n "$PID" ]; then
-
- echo "[stop] $BASE_PATH/$SERVER_NAME 运行中,开始 kill [$PID]"
- kill -15 $PID
-
- for ((i = 0; i < 120; i++))
- do
- sleep 1
- PID=$(ps -ef | grep $BASE_PATH/$SERVER_NAME | grep -v "grep" | awk '{print $2}')
- if [ -n "$PID" ]; then
- echo -e ".\c"
- else
- echo "[stop] 停止 $BASE_PATH/$SERVER_NAME 成功"
- break
- fi
- done
-
- if [ -n "$PID" ]; then
- echo "[stop] $BASE_PATH/$SERVER_NAME 失败,强制 kill -9 $PID"
- kill -9 $PID
- fi
-
- else
- echo "[stop] $BASE_PATH/$SERVER_NAME 未启动,无需停止"
- fi
- }
- function start() {
-
- echo "[start] 开始启动 $BASE_PATH/$SERVER_NAME"
- echo "[start] JAVA_OPS: $JAVA_OPS"
- echo "[start] JAVA_AGENT: $JAVA_AGENT"
- echo "[start] PROFILES: $PROFILES_ACTIVE"
-
- BUILD_ID=dontKillMe nohup java -server $JAVA_OPS $JAVA_AGENT -jar $BASE_PATH/$SERVER_NAME.jar --spring.profiles.active=$PROFILES_ACTIVE &
- echo "[start] 启动 $BASE_PATH/$SERVER_NAME 完成"
- }
- function healthCheck() {
-
- if [ -n "$HEALTH_CHECK_URL" ]; then
-
- echo "[healthCheck] 开始通过 $HEALTH_CHECK_URL 地址,进行健康检查";
- for ((i = 0; i < 120; i++))
- do
-
- result=`curl -I -m 10 -o /dev/null -s -w %{http_code} $HEALTH_CHECK_URL || echo "000"`
-
- if [ "$result" == "200" ]; then
- echo "[healthCheck] 健康检查通过";
- break
-
- else
- echo -e ".\c"
- sleep 1
- fi
- done
-
- if [ ! "$result" == "200" ]; then
- echo "[healthCheck] 健康检查不通过,可能部署失败。查看日志,自行判断是否启动成功";
- tail -n 10 nohup.out
- exit 1;
-
- else
- tail -n 10 nohup.out
- fi
-
- else
- echo "[healthCheck] HEALTH_CHECK_URL 未配置,开始 sleep 120 秒";
- sleep 120
- echo "[healthCheck] sleep 120 秒完成,查看日志,自行判断是否启动成功";
- tail -n 50 nohup.out
- fi
- }
- function deploy() {
- cd $BASE_PATH
-
- backup
-
- stop
-
- transfer
-
- start
-
- healthCheck
- }
- deploy
|