Java 应用 JAR 包部署启动脚本

目录结构

1
2
3
4
5
6
7
8
9
10
[txrd@host-148 /data/user-center]$ tree -af 
.
├── ./project
│ └── ./user-center-server.jar
└── ./logs
│ └── ./info.log
│ └── ./error.log
│ └── ./warn.log
│ └── ./stdout.log
└── ./restart.sh

restart.sh 脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env bash

# @Author: YL
# @Date: 2019-05-08 15:52:30
# @Last Modified by: YL
# @Last Modified time: 2022-05-21 13:28:19

export LANG=en_US.UTF-8

cd `dirname $0`
CURRENT_DIR=`pwd`
LOGS_DIR=$CURRENT_DIR/logs
STDOUT_FILE=$CURRENT_DIR/logs/stdout.log
PROJECT_HOME=$CURRENT_DIR/project
JAR_FILE=$PROJECT_HOME/user-center-server.jar

mkdir -p ${LOGS_DIR}

# pid
PIDS=`ps -ef | grep java | grep -v grep | grep "$JAR_FILE" | awk '{print $2}'`

# JVM options
JAVA_OPT="-server -Xms1g -Xmx1g -Xss256k"
JAVA_OPT="${JAVA_OPT} -Dloader.path=${PROJECT_HOME}/lib/ -Djava.io.tmpdir=${LOGS_DIR}"
JAVA_OPT="${JAVA_OPT} -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:LargePageSizeInBytes=128m -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=70"
JAVA_OPT="${JAVA_OPT} -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${LOGS_DIR}/dump-`date +"%Y-%m-%d"`.hprof"
JAVA_OPT="${JAVA_OPT} -Ddubbo.protocol.port=20886"
JAVA_OPT="${JAVA_OPT} -Dspring.profiles.active=pro -Dfile.encoding=UTF-8"
JAVA_OPT="${JAVA_OPT} -jar ${JAR_FILE}"

# Graceful shutdown
function gracefulShutdown(){
if [ -z "$PIDS" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE does not started!" | tee -a $STDOUT_FILE
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE kill $PIDS begining" | tee -a $STDOUT_FILE
for PID in $PIDS ; do
kill $PID > /dev/null 2>&1
done

# check for graceful shutdown
COUNT=0
while [ $COUNT -lt 1 ]; do
sleep 1
COUNT=1
for PID in $PIDS ; do
PID_EXIST=`ps -f -p $PID | grep java`
if [ -n "$PID_EXIST" ]; then
COUNT=0
break
fi
done
done
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE kill $PID success" | tee -a $STDOUT_FILE
fi
}

function operate(){
if [[ "$1" = "kill" ]]; then
gracefulShutdown
elif [[ "$1" = "start" ]] ; then
# check JAVA_HOME ENV
if [[ -z ${JAVA_HOME} ]]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE 未配置 JAVA_HOME 环境变量" | tee -a $STDOUT_FILE
exit 1
fi
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE JAVA_HOME=${JAVA_HOME}" | tee -a $STDOUT_FILE

cd $PROJECT_HOME
# starting
nohup $JAVA_HOME/bin/java $JAVA_OPT >> $STDOUT_FILE 2>&1 &
PIDS=`ps -ef | grep java | grep -v grep | grep "$JAR_FILE" | awk '{print $2}'`
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE started OK! pid: $PIDS" | tee -a $STDOUT_FILE
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE is not support $1" | tee -a $STDOUT_FILE
fi
}

if [[ "$1" = "start" || "$1" = "check" ]]; then
if [ -n "$PIDS" ]; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE already started! pid: $PIDS" >> $STDOUT_FILE
exit 1
fi
operate start
elif [[ "$1" = "" || "$1" = "restart" ]]; then
operate kill
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE starting" | tee -a $STDOUT_FILE
ls -d -t $LOGS_DIR/undertow* | tail -n +3 | xargs rm -rf
operate start
elif [[ "$1" = "kill" ]]; then
operate kill
else
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $JAR_FILE is not support $1" | tee -a $STDOUT_FILE
fi
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/42919bde.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!