-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathminecraft_server
executable file
·423 lines (312 loc) · 7.5 KB
/
minecraft_server
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/bin/sh
### BEGIN INIT INFO
# Provides: minecraft_server
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# chkconfig: 345 50 50
# Description: Minecraft Server Control Script
### END INIT INFO
#
# startscript for spigot minecraft server
#
# (c) 2015-2016 nimmis <[email protected]>
#
# fetch global variables when starting from supervisord
. /etc/container_environment.sh
MC_USER=minecraft # User name running the minecraft server
MC_PROC=spigot.jar # name of minecraft jar file
MC_DIR=/minecraft/ # Directory where the server should run
if [ -z "$MC_MAXMEM" ]; then
MC_MAXMEM="1G"
fi
if [ -z "$MC_MINMEM" ]; then
MC_MINMEM=$MC_MAXMEM ;
fi
MC_JAVA_OPS="-Xmx$MC_MAXMEM -Xms$MC_MINMEM" # java options for minecraft server
JAVACMD=$(which java)
USAGE=$(cat <<EOF
Usage: $0 <option>
Options:
start
Start the Minecraft server.
stop
Stop the Minecraft server.
restart
Restart the Minecraft server.
create <version>
Create a minecraft server, version is the version of minecraft you want to use.
latest gives the latest version. Does not clean other files in the directory
status
Give the status of the server
accept-eula
Accepts Mojangs EULA and set eula.txt correct
send "<command>"
Sends a command to the Minecraft server
log
show output log from the Minecraft server
backup
Makes a backup of the world
EOF
)
#
# get the PID for minecraft server pocess
#
getPID() {
local PID
PID=$( ps -f -u $MC_USER | grep "$MC_PROC" | grep -v "sh -c" | grep -v grep | awk '{print $2}' )
printf "%d\n" $PID
}
#
# return 1 if the minecraft server is running othervise 0
#
isRunning() {
if [ $(getPID) -eq 0 ] ; then
echo 0
else
echo 1
fi
}
# execute command as minecraft user
execCMD() {
# if running as root, switch to defined user
if [ $(id -u) -eq 0 ]; then
su -s /bin/sh -c "$1" $MC_USER $2
else
sh -c "$1" $2
fi
}
#
# setProp
#
# setProp <filename> <property> <value>
#
#
setProp() {
grep -q "^$2\s*\=" $1
if [ $? -ne 0 ] ; then
echo "$prop=$val" >> $1
else
sed -i "/^$2\s*=/ c $2=$3" $1
fi
}
#
# send a command to the running minecraft server
sendCMD() {
echo $1 >> $MC_DIR/input.con
}
#
# create eula.txt if it does not exist
#
createEULA() {
if [ ! -f $MC_DIR/eula.txt ] ; then
execCMD "echo '#EULA file created by minecraft script\neula=false' > $MC_DIR/eula.txt"
fi
}
#
# EULA accepted (0) or not (1)
#
acceptEULA() {
local OK=1
grep eula $MC_DIR/eula.txt | grep -q 'true' && OK=0
echo "$OK"
}
#
# check that all is ok the start server
#
checkOK() {
# check that the directory exists
if [ ! -d $MC_DIR ] ; then
echo "$MC_DIR does not exist" ;
exit 1
fi
# and there are a jar file in it
if [ ! -f $MC_DIR/$MC_PROC ] ; then
echo "the minecraftfile $MC_DIR/$MC_PROC does not exist."
exit 1
fi
# check to see that minecraft user can write in the
execCMD "touch $MC_DIR/testfile 2> /dev/null"
if [ ! -f $MC_DIR/testfile ] ; then
echo "the user $MC_USER has not write access to directory $MC_DIR"
exit 1
else
rm $MC_DIR/testfile
fi
# check if EULA accepted
createEULA
if [ $(acceptEULA) -eq 1 ] ; then
echo "you must accept Mojangs EULA, either set eula=true in $MC_DIR/eula.txt"
echo "or run $0 accept-eula"
exit 1
fi
}
#
# create spigot jar for requested version
#
buildjar() {
# build it if it not exists
if [ ! -f $SPIGOT_HOME/spigot-$1.jar ]; then
echo "Building spigot version ($1) jar file, be patient"
if [ -d $SPIGOT_HOME/build ]; then
rm -Rf $SPIGOT_HOME/build
fi
mkdir -p $SPIGOT_HOME/build
wget https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar -O $SPIGOT_HOME/build/BuildTools.jar
HOME=$SPIGOT_HOME/build
export HOME
cd $HOME
git config --global --unset core.autocrlf
java -jar BuildTools.jar --rev $1
cp $SPIGOT_HOME/build/Spigot/Spigot-Server/target/spigot-*.jar $SPIGOT_HOME/spigot-$1.jar
cd $SPIGOT_HOME/
# grab the minecraft server version, download the minecraft client for overviewer
rm -Rf /overviewer/jar
mkdir -p /overviewer/jar
MC_VERSION=`ls -1 /$SPIGOT_HOME/build/work/minecraft_server.*.jar | cut -d'.' -f2- | sed 's/\(.*\)\..*/\1/'`
export MC_VERSION
wget https://s3.amazonaws.com/Minecraft.Download/versions/${MC_VERSION}/${MC_VERSION}.jar -P /overviewer/jar
ls -1t /overviewer/jar | grep -v latest | head -1 | xargs -I{} ln -s {} /overviewer/jar/minecraft_client.latest.jar
chmod -R 777 /overviewer
rm -Rf $SPIGOT_HOME/build
fi
execCMD "rm -f $SPIGOT_HOME/spigot.jar"
execCMD "ln -s $SPIGOT_HOME/spigot-$1.jar $SPIGOT_HOME/spigot.jar"
}
#
# prepare console files
#
ResetConsoleFiles() {
rm -f $MC_DIR/input.con
rm -f $MC_DIR/output.con
if [ -f $MC_DIR/input.con ]; then
echo "$MC_DIR/input.con could not be removed"
exit 1
fi
if [ -f $MC_DIR/output.con ]; then
echo "$MC_DIR/output.con could not be removed"
exit 1
fi
execCMD "touch $MC_DIR/input.con"
execCMD "touch $MC_DIR/output.con"
if [ ! -f $MC_DIR/input.con ]; then
echo "$MC_DIR/input.con could not be created by $MC_USER"
exit 1
fi
if [ ! -f $MC_DIR/output.con ]; then
echo "$MC_DIR/output.con could not be created by $MC_USER"
exit 1
fi
}
#
# start the minecraft server
#
start() {
echo -n "Starting minecraft server as user $MC_USER..."
checkOK
ResetConsoleFiles
# be in right working directory when starting
cd $MC_DIR
execCMD "tail -f --pid=\$$ $MC_DIR/input.con | { $JAVACMD $MC_JAVA_OPS -jar $MC_DIR/$MC_PROC nogui > $MC_DIR/output.con 2>&1 ; kill \$$ ; } " &
# check if the command wait OK
if [ $? -ne 0 ]; then
echo "Could not start $MC_DIR/$MC_PROC.\n"
exit 1
fi
# wait 2 seconds to see if the process survived
sleep 2
if [ $(isRunning) -eq 1 ] ; then
echo "Started"
else
echo "stopped again"
exit 1
fi
}
stop() {
local i
echo -n "Stopping minecraft server..."
sendCMD "stop"
# wait for 10 seconds until killing it
i=0
while [ $i -lt 10 -a $(isRunning) -eq 1 ]
do
echo -n "."
sleep 1
i=`expr $i + 1`
done
if [ $(isRunning) -eq 1 ] ; then
echo -n "(need to kill it)..."
kill $(getPID)
# give it time to die
sleep 2
fi
if [ $(isRunning) -eq 1 ] ; then
echo "Could not stop it"
exit 1
else
echo "Stopped"
fi
}
seteula() {
# make sure that we have an eula.txt file to modifie
createEULA
setProp $MC_DIR/eula.txt "eula" "true"
echo "EULA accepted"
}
case "$1" in
start)
if [ $(isRunning) -eq 1 ] ; then
echo -n "$MC_PROC is already running with PID "
getPID
exit 1
fi
start
;;
stop)
if [ $(isRunning) -eq 1 ] ; then
stop
else
echo "$MC_PROC is not running"
fi
;;
restart)
stop
start
;;
create)
SVER=$2
if [ -z "$SVER" ] ; then
SVER="latest"
fi
echo "Setting version to $SVER"
buildjar $SVER
;;
status)
if [ $(isRunning) -eq 1 ] ; then
echo -n "$MC_PROC is running with PID "
getPID
else
echo "$MC_PROC is not running"
fi
;;
accept-eula)
seteula
;;
send)
sendCMD "$2"
;;
log)
echo "Abort with CTRL-C"
tail -f -n 50 $MC_DIR/output.con
;;
console)
;;
backup)
;;
*)
printf "$USAGE\n"
exit 1
;;
esac
exit 0