-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.sh
More file actions
executable file
·370 lines (341 loc) · 11.4 KB
/
server.sh
File metadata and controls
executable file
·370 lines (341 loc) · 11.4 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/bin/bash
##########################
#
# Java Server startscript
#
# ver 3.15
#
#########################
# Color Codes
WHITE='\033[0;37m'
GREEN='\033[0;32m'
LRED='\033[1;31m'
YELLOW='\033[1;33m'
# Some helper string. Don't edit them!
PARAM2=$2
VERSION=3.15
############################################################################ Checking Config files ###############################
# config file for tail files
if [ -e "srv_cfg_loglines.dat" ]; then
LOGLINES=`cat srv_cfg_loglines.dat | awk ' {print $1 }' | grep '^[0-9]'`
if [ -z $LOGLINES ] || [ $LOGLINES -lt 5 ]; then
printf "${YELLOW}Warning:${GREEN} Your tail log lines are too small at least should be 5.\n"
printf "40">srv_cfg_loglines.dat
printf "${GREEN}"
printf "Tail log lines set to default ${LRED}40${GREEN} in \"${LRED}srv_cfg_loglines.dat${GREEN}\"\n"
LOGLINES=40
printf "${WHITE}"
fi
else
printf "${YELLOW}Warning:${GREEN} \"${LRED}srv_cfg_loglines.dat${GREEN}\" missing.\Creating.\n\n"
printf "40">srv_cfg_loglines.dat
printf "${WHITE}"
LOGLINES=15
fi
# config file for wait to stop server
if [ -e "srv_cfg_stopsec.dat" ]; then
STOPSEC=`cat srv_cfg_stopsec.dat | awk ' {print $1 }' | grep '^[0-9]'`
if [ -z $STOPSEC ] || [ $STOPSEC -lt 5 ]; then
printf "${YELLOW}Warning:${GREEN} Your stop waiting seconds are too small at least should be 5.\n"
printf "5">srv_cfg_stopsec.dat
printf "${LRED}"
printf "Stop seconds set to default 15 sec in \"srv_cfg_stopsec.dat\"\n"
STOPSEC=15
printf "${WHITE}"
fi
else
printf "${YELLOW}Warning:${GREEN} \"${LRED}srv_cfg_stopsec.dat${GREEN}\" missing.\Creating.\n\n"
printf "15">srv_cfg_stopsec.dat
printf "${WHITE}"
STOPSEC=15
fi
# config file for java command line (may you can run in docker or another jail)
if [ -e "srv_cfg_servercmd.dat" ]; then
#SZERVER=`cat srv_cfg_servercmd.dat | awk ' {print $1 }' | grep '^[A-Z,a-z,0-9]'`
CMD=`cat srv_cfg_servercmd.dat`
else
printf "${GREEN}"
printf "${YELLOW}Warning:${GREEN} \"${LRED}srv_cfg_serverparam.dat${GREEN}\" missing.\nCreating with default value: \"${GREEN}java -Xms1024m -Xmx1024m -Dfile.encoding=UTF-8 -jar${GREEN}\".\n"
printf "java -Xms1024m -Xmx1024m -Dfile.encoding=UTF-8 -jar" srv_cfg_servercmd.dat
printf "${WHITE}\n"
CMD='java -Xms1024m -Xmx1024m -Dfile.encoding=UTF-8 -jar'
fi
# config file for start stop java server
if [ -e "srv_cfg_servername.dat" ]; then
#SZERVER=`cat srv_cfg_servername.dat | awk ' {print $1 }' | grep '^[A-Z,a-z,0-9]'`
SZERVER=`cat srv_cfg_servername.dat`
else
printf "${GREEN}"
printf "${LRED}Error:${GREEN} \"${LRED}srv_cfg_servername.dat${GREEN}\" missing.\nExiting.\n\n"
printf "Create and insert your server program filenname without extension in config file before start using this program.\n"
printf "Exiting.\n\n"
printf "${WHITE}\n"
exit 1
fi
# config file for server progam parameter(s)
if [ -e "srv_cfg_serverparam.dat" ]; then
#SZERVER=`cat srv_cfg_serverparam.dat | awk ' {print $1 }' | grep '^[A-Z,a-z,0-9]'`
SRVPARAM=`cat srv_cfg_serverparam.dat`
else
printf "${GREEN}"
printf "${YELLOW}Warning:${GREEN} \"${LRED}srv_cfg_serverparam.dat${GREEN}\" missing.\nCreating.\n"
touch srv_cfg_serverparam.dat
printf "${WHITE}\n"
SRVPARAM=''
fi
############################################################################ Functions ###########################################
start(){
# start script
if ! chkrun; then
rm $SZERVER.pid.old 2>/dev/null
printf "${GREEN}"
printf "Starting $SZERVER server in background ...\n"
SRVCMD="$CMD $SZERVER.jar $SRVPARAM >$SZERVER.log &"
eval "$SRVCMD"
echo $! > $SZERVER.pid
# java -Xms1024m -Xmx1024m -Dfile.encoding=UTF-8 -jar $SZERVER.jar $SRVPARAM >$SZERVER.log & echo $! > $SZERVER.pid
printf "PID: "
cat $SZERVER.pid
else
printf "${GREEN}$SZERVER server is running already with PID: "
cat $SZERVER.pid
printf "Not starting it again."
fi
printf "${WHITE}\n"
}
debug(){
if ! chkrun; then
SRVCMD="$CMD $SZERVER.jar"
eval "$SRVCMD"
else
printf "${GREEN}$SZERVER server is running already with PID: "
cat $SZERVER.pid
printf "Not starting it again."
fi
printf "${WHITE}\n"
}
stop() {
#stop sctipt
sec=0
if [ -e $SZERVER.pid ]; then
pid=`cat $SZERVER.pid`
else
printf "${LRED}Error:${GREEN} Pid file missing can't stop server or not running.\nExitig.\n"
exit 1
fi
if [ -e /proc/$pid ]; then
CMDLINE=`cat /proc/$pid/cmdline`
SERVERNAME=$SZERVER.jar
if [[ $CMDLINE = *java* && $CMDLINE = *$SERVERNAME* ]]; then
# if [ -z "${CMDLINE##*"java"*}" ] && [ -z "${CMDLINE##*$SERVERNAME*}" ]; then
printf "${GREEN}$SZERVER Server is running, Stopping it.\n"
sleep 1;
kill -15 $pid;
printf "Wait up to $STOPSEC second(s) for stop server...\n"
while ps -p $pid > /dev/null; do
sleep 1;
sec=$((sec + 1));
if [ $sec -eq $STOPSEC ]; then
printf "Timeout. Terminate stopping script. Check for dead thread(s)."
printf "$SZERVER server PID: "
cat $SZERVER.pid
printf "${WHITE}\n"
exit 1
fi
done;
printf "Last 15 line from log:\n"
printf "${WHITE}\n"
tail -n 15 $SZERVER.log
sleep 2
printf "${GREEN}\n"
rm $SZERVER.pid.old 2>/dev/null
cp $SZERVER.pid $SZERVER.pid.old >/dev/null
rm $SZERVER.pid 2>/dev/null
printf "${WHITE}"
fi
fi
}
######################################################################### Log file Functions ###########################################
log(){
if [ -e "$SZERVER.log" ]; then
printf "${GREEN}"
printf "Listing actual log file:\n"
printf "Mode: "
case "$PARAM2" in
f)
printf "follow - list last $LOGLINES lines of log and follow changes. Exit with ctrl+c\n"
printf "${WHITE}"
tail -n$LOGLINES -f $SZERVER.log
exit 1
;;
t)
printf "tail - list last $LOGLINES lines of log.\n"
printf "${WHITE}"
tail -n$LOGLINES $SZERVER.log
exit 1
;;
*)
# no param cat log file
printf "fullog - print whole log file"
cat "$SZERVER.log"
printf "\n"
;;
esac
else
printf "${GREEN}"
printf "${LRED}Error:${GREEN} \"${LRED}$SZERVER.log${GREEN}\" missing.\nExiting.\n\n"
printf "${WHITE}"
exit 1
fi
printf "${WHITE}"
}
############################################################################ Help Screen ###########################################
help(){
printf "\n"
printf "${GREEN}"
printf "Java Server program manager\n"
printf "\n"
printf "Help screen\n"
printf "\n"
syntax
printf "\n"
printf "Params:\n"
printf "\n"
printf "${LRED}start${GREEN} Start server in background\n"
printf "${LRED}stop${GREEN} Stop background running server\n"
printf "${LRED}restart${GREEN} Restart bacground running server\n"
printf "${LRED}debug${GREEN} Start server in foreground. May stop server with crtl+c\n"
printf "${LRED}status${GREEN} Checking server (running) status. \n"
printf "${LRED}log${GREEN} Show full log of server\n"
printf "${LRED}log t${GREEN} Tail of log file ( last $LOGLINES lines )\n"
printf "${LRED}log f${GREEN} Tail of log file and follow changes. Start with last $LOGLINES lines. Stop with ctrl+c key.\n"
printf "${LRED}chkconfig${GREEN} Check your configuration\n"
printf "${LRED}ver${GREEN} Check script version, and latest release on Github\n"
printf "${LRED}help${GREEN} This screen\n"
printf "\n"
printf "\n"
printf "${WHITE}Config files:${GREEN}\n"
printf "\n"
printf "These files editable by you."
printf "\n"
printf "${LRED}srv_cfg_servercmd.dat${GREEN} Your command for run your server.\n"
printf " CMDline will looks like this when you issue start:${YELLOW} srv_cfg_servercmd.dat srv_cfg_servername.dat ...${GREEN}\n"
printf " check ${LRED}chkconfig${GREEN} param for exact cmdline!\n"
printf " Default value is: ${LRED}java -Xms1024m -Xmx1024m -Dfile.encoding=UTF-8 -jar${GREEN}\n"
printf "${LRED}srv_cfg_servername.dat${GREEN} Your server program (file)name without extension.\n"
printf " (e.g. MyServer) itt will start MyServer.jar and create MyServer.pid and MyServer.log\n"
printf " No default value program will exit with error if file is missing.\n"
printf "${LRED}srv_cfg_serverparam.dat${GREEN} Insert your parameters into this config file e.g. --noconsole \n"
printf " ${YELLOW}Warning!!!${GREEN} No parameters passing trough from this file for debug start!\n"
printf " CMDline will looks like this when you issue start:${YELLOW} ...... srv_cfg_servername.dat srv_cfg_serverparam.dat${GREEN}\n"
printf " check ${LRED}chkconfig${GREEN} param for exact cmdline!\n"
printf "${LRED}srv_cfg_stopsec.dat${GREEN} Wait seconds for stop server before this script exit with warn you about dead process.\n"
printf " Only numbers in config file. (e.g 10) minimum is 5 second, default 15 second.\n"
printf "${LRED}srv_cfg_loglines.dat${GREEN} How many last lines show from log file.\n"
printf " Only numbers in config file. (e.g 40) minimum value is 5, default value is 40.\n"
printf "${WHITE}\n"
}
syntax(){
printf "${WHITE}Syntax: ${GREEN}$0 ${WHITE}{ ${LRED}start${WHITE} | ${LRED}stop${WHITE} | ${LRED}restart${WHITE} | ${LRED}debug${WHITE} | ${LRED}status${WHITE} | ${LRED}log${WHITE} [${LRED}f${WHITE}|${LRED}t${WHITE}] | ${LRED}chkconfig${WHITE} | ${LRED}help${WHITE} | ${LRED}ver${WHITE} }\n"
}
version(){
printf "Version: ${GREEN}$VERSION${WHITE}\n"
printf "Wait a sec, checking for latest release on Github.\n"
RELEASE=`curl --silent "https://api.github.com/repos/larryl79/Spigot-startscript/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'`
echo "Git release: ${GREEN}$RELEASE${WHITE}"
if [ $RELEASE \> $VERSION ]; then
printf "${YELLOW}Warning: ${GREEN}You are not on a latest release, ${YELLOW}please update${GREEN} for new feauters and bugfixes from github.\n"
printf "\n"
else
printf "${GREEN}You are on a latest release.\n"
fi
printf "${WHITE}"
}
chkconfig(){
printf "${GREEN}Check configuration.\n"
printf "\n"
printf "srv_cfg_servername = $SZERVER\n"
printf "srv_cfg_serverparam = $SRVPARAM\n"
printf "srv_cfg_cmd = $CMD\n"
printf "srv_cfg_loglines = $LOGLINES\n"
printf "srv_cfg_stopsec = $STOPSEC\n"
printf "\n"
printf "${GREEN}Your START command line by config:${WHITE}\n"
printf "$CMD $SZERVER.jar $SRVPARAM\n"
printf "${GREEN}Your DEBUG command line by config:${WHITE}\n"
printf "$CMD $SZERVER.jar\n"
printf "\n"
}
chkrun(){
if [ -e $SZERVER.pid ]; then
pid=`cat $SZERVER.pid`
if [ -e "/proc/$pid/cmdline" ]; then
CMDLINE=`cat /proc/$pid/cmdline`
SERVERNAME=$SZERVER.jar
# if [ -z "${CMDLINE##*"java"*}" ] && [ -z "${CMDLINE##*$SERVERNAME*}" ]; then
# if [ $CMDLINE == *_"java"_* ]; then
#&& [ $CMDLINE = *"$SERVERNAME"* ]; then
if [[ $CMDLINE = *java* && $CMDLINE = *$SERVERNAME* ]]; then
#true
return 0
else
# fase
return 1
fi
else
# false
# rm $SZERVER.pid 2>/dev/null
return 1
fi
else
# false
return 1
fi
}
status(){
if chkrun; then
printf "${GREEN}$SZERVER Server is running with PID: "
cat $SZERVER.pid
else
printf "${GREEN}Server not runnning.\n"
fi
printf "${WHITE}"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
debug)
debug
;;
log)
log
;;
help)
help
;;
ver)
version
;;
chkconfig)
chkconfig
;;
*)
printf "Server program manager\n"
printf "\n"
syntax
printf "\n"
exit 1
;;
esac
exit 0