-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstart.sh
executable file
·132 lines (109 loc) · 2.95 KB
/
start.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
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
#!/bin/bash
###### ######## ####### ####### ### #######
### ## ## ## ###
### ####### ###### ### ## ### #######
### ### ## ## ## ### ## ### ##
###### ### ## ## ## ###### ### ##
# arguments
SERVER=$1
JSON_FILE=$2
###########
# Imports #
###########
# status file import
STATUS_FILE=$(jq -r ".files.status_file" "$JSON_FILE")
# statuses import
declare -A STATUS=(
["on"]="$(jq -r ".statuses.on" "$JSON_FILE")"
["run"]="$(jq -r ".statuses.run" "$JSON_FILE")"
["res"]="$(jq -r ".statuses.res" "$JSON_FILE")"
["off"]="$(jq -r ".statuses.off" "$JSON_FILE")"
["err"]="$(jq -r ".statuses.err" "$JSON_FILE")"
)
# log file import
LOG_FILE=$(jq -r ".files.logs_file" "$JSON_FILE")
# ram import
MIN_RAM=$(jq -r ".servers.${SERVER}.min_ram" "$JSON_FILE")
MAX_RAM=$(jq -r ".servers.${SERVER}.max_ram" "$JSON_FILE")
# parameters import
PARAMETER_ID=$(jq -r ".servers.${SERVER}.parameter_id" "$JSON_FILE")
PARAMETERS=$(jq -r ".parameters[${PARAMETER_ID}]" "$JSON_FILE")
# Jar import
JAR_FILE=$(jq -r ".servers.${SERVER}.jar_file" "$JSON_FILE")
if [ "$JAR_FILE" == null ]
then
JAR_FILE=""
else
JAR_FILE="-jar ${JAR_FILE}"
fi
#############
# Functions #
#############
# logging handling
function fn_to_log() {
echo "[$(date)] : ${1}" >> "$LOG_FILE"
}
# change the server status
function fn_change_status() {
echo "$1" > "$STATUS_FILE"
}
# move to server directory if present
cd "$SERVER" || exit 1
#################
# Crash handler #
#################
# timestamp and tries variables
TIME_STAMP=0
MAX_TRIES=3
TEST_TIME=500
TRIES=$MAX_TRIES
function fn_timer_update() {
if [ $(($(date +%s) - "$TIME_STAMP")) -ge $TEST_TIME ]
then
TIME_STAMP=$(date +%s)
TRIES=$MAX_TRIES
else
((TRIES -= 1))
fi
if [ $TRIES -le 0 ]
then
fn_change_status "${STATUS[err]}"
fi
}
#######################
# Server handler loop #
#######################
while true
do
case $(cat "$STATUS_FILE")
in
"${STATUS[on]}")
fn_to_log "Server started."
fn_change_status "${STATUS[run]}"
fn_timer_update
java -Xmx${MAX_RAM} -Xms${MIN_RAM} ${PARAMETERS} ${JAR_FILE}
;;
"${STATUS[run]}")
fn_to_log "Server closed or crashed, restarting it..."
fn_change_status "${STATUS[on]}"
;;
"${STATUS[res]}")
fn_to_log "Server restarted."
fn_change_status "${STATUS[on]}"
;;
"${STATUS[off]}")
fn_to_log "Server stopped."
exit 0
;;
"${STATUS[err]}")
fn_to_log "Server crashed multiple times, shutting it down..."
fn_change_status "${STATUS[off]}"
;;
*)
fn_to_log "ERROR Start script."
fn_to_log "Status file -> $(cat "$STATUS_FILE")"
rm "$STATUS_FILE"
exit 1
;;
esac
done