-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathstart.sh.simple
More file actions
192 lines (164 loc) · 4.7 KB
/
start.sh.simple
File metadata and controls
192 lines (164 loc) · 4.7 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#!/bin/bash
#############################################################################
#
# GNU LESSER GENERAL PUBLIC LICENSE
# Version 3, 29 June 2007
#
# Copyright (C) [2007] [TRON Foundation], Inc. <https://fsf.org/>
# Everyone is permitted to copy and distribute verbatim copies
# of this license document, but changing it is not allowed.
#
#
# This version of the GNU Lesser General Public License incorporates
# the terms and conditions of version 3 of the GNU General Public
# License, supplemented by the additional permissions listed below.
#
# You can find java-tron at https://github.com/tronprotocol/java-tron/
#
##############################################################################
# TRON Full Node Management Simple Script
#
# NOTE: This is a simple and concise script to start and stop the java-tron full node,
# designed for developers to quickly get started and learn.
# It may not be suitable for production environments.
#
# Usage:
# sh start.sh # Start the java-tron FullNode
# sh start.sh -s # Stop the java-tron FullNode
# sh start.sh [options] # Start with additional java-tron options,such as: -c config.conf -d /path_to_data, etc.
#
##############################################################################
# adjust JVM start
# Set the maximum heap size to 9G, adjust as needed
VM_XMX="9G"
# adjust JVM end
FULL_NODE_JAR="FullNode.jar"
FULL_START_OPT=()
PID=""
MAX_STOP_TIME=60
JAVACMD=""
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
log() {
local level="$1"; shift
local timestamp color=""
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
case "$level" in
INFO) color="$GREEN" ;;
WARN) color="$YELLOW" ;;
ERROR) color="$RED" ;;
esac
printf "%b[%s] [%s]:%b %s\n" "$color" "$timestamp" "$level" "$NC" "$*" | tee -a "${SCRIPT_DIR}/start.log"
}
info() { log INFO "$@"; }
warn() { log WARN "$@"; }
error() { log ERROR "$@"; }
die() { error "$@"; exit 1; }
ulimit -n 65535 || warn "Failed to set ulimit -n 65535"
findJava() {
if [ -n "${JAVA_HOME:-}" ]; then
if [ -x "$JAVA_HOME/jre/sh/java" ]; then
JAVACMD="$JAVA_HOME/jre/sh/java"
else
JAVACMD="$JAVA_HOME/bin/java"
fi
[ -x "$JAVACMD" ] || die "JAVA_HOME is invalid: $JAVA_HOME"
else
JAVACMD="java"
which java >/dev/null 2>&1 || die "JAVA_HOME not set and no 'java' in PATH"
fi
"$JAVACMD" -version > /dev/null 2>&1 || die "Java command not working"
}
checkPid() {
# shellcheck disable=SC2009
PID=$(ps -ef |grep $FULL_NODE_JAR |grep -v grep |awk '{print $2}')
}
stopService() {
checkPid
if ! kill -0 "$PID" 2>/dev/null; then
info "java-tron is not running."
return 0
fi
info "Stopping java-tron service (PID: $PID)"
local count=1
while [ -n "$PID" ] && [ $count -le $MAX_STOP_TIME ]; do
kill -TERM "$PID" 2>/dev/null && info "Sent SIGTERM to java-tron (PID: $PID), attempt $count"
sleep 1
checkPid
count=$((count + 1))
done
if [ -n "$PID" ]; then
warn "Forcing kill java-tron (PID: $PID) after $MAX_STOP_TIME seconds"
kill -KILL "$PID" 2>/dev/null
sleep 1
checkPid
fi
if [ -n "$PID" ]; then
die "Failed to stop the service (PID: $PID)"
else
info "java-tron stopped"
wait_with_info 2 "Cleaning up..."
fi
}
startService() {
if [ -n "${FULL_START_OPT[*]}" ]; then
info "Starting java-tron service with options: ${FULL_START_OPT[*]}"
fi
if [ ! -f "$FULL_NODE_JAR" ]; then
die "$FULL_NODE_JAR not found in path $SCRIPT_DIR."
fi
nohup "$JAVACMD" \
-Xmx"$VM_XMX" \
-XX:+UseZGC \
-Xlog:gc,gc+heap:file=gc.log:time,tags,level:filecount=10,filesize=100M \
-XX:ReservedCodeCacheSize=256m \
-XX:+UseCodeCacheFlushing \
-XX:MetaspaceSize=256m \
-XX:MaxMetaspaceSize=512m \
-XX:MaxDirectMemorySize=1g \
-XX:+HeapDumpOnOutOfMemoryError \
-jar "$FULL_NODE_JAR" "${FULL_START_OPT[@]}" \
>> start.log 2>&1 &
info "Waiting for the service to start..."
wait_with_info 5 "Starting..."
checkPid
if [ -n "$PID" ]; then
info "Started java-tron with PID $PID on $HOSTNAME."
else
die "Failed to start java-tron, see start.log or logs/tron.log for details."
fi
}
wait_with_info() {
local seconds=$1
local message=$2
for i in $(seq "$seconds" -1 1); do
info "$message wait ($i) s"
sleep 1
done
}
start() {
checkPid
if [ -n "$PID" ]; then
info "java-tron is already running (PID: $PID), to stop the service: sh start.sh -s"
return
fi
findJava
startService
}
while [ -n "$1" ]; do
case "$1" in
-s)
stopService
exit 0
;;
*)
FULL_START_OPT+=("$@")
break
;;
esac
done
start
exit 0