This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathminecraft
executable file
·588 lines (512 loc) · 18.2 KB
/
minecraft
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/bin/bash
# /etc/init.d/minecraft
# version 2014-08-16 (YYYY-MM-DD)
### BEGIN INIT INFO
# Provides: minecraft
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Should-Start: $network
# Should-Stop: $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Minecraft server
# Description: Starts the CraftBukkit Minecraft server
### END INIT INFO
# minecraft-init-script - An initscript to start Minecraft or CraftBukkit
# Copyright (C) 2011-2014 Super Jamie <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Source the function library
## CentOS/Fedora
if [ -f /etc/rc.d/init.d/functions ]
then
. /etc/rc.d/init.d/functions
fi
## Ubuntu
if [ -f /lib/lsb/init-functions ]
then
. /lib/lsb/init-functions
fi
<<COMMENT
## ---------------------------------------------------------
## copy between these lines and place them at /etc/default/minecraft
## do not include the <<COMMENT and COMMENT words
## Settings for minecraft-init-script
# Nice looking name of service for script to report back to users
SERVERNAME="CraftBukkit"
# Filename of server binary
SERVICE="craftbukkit.jar"
# URL of server executable for update checking - http://cbukk.it/craftbukkit.jar is the recommended latest Craftbukkit URL
SERVER_URL="http://cbukk.it/craftbukkit.jar"
# Username of non-root user who will run the server
USERNAME="bukkit"
# Path of server binary and world
MCPATH="/home/bukkit/craftbukkit"
# Number of CPU cores to thread across if using multithreaded garbage collection
CPU_COUNT="2"
# Where backups go
BACKUPPATH="/home/bukkit/backups"
# How many days worth of backups to keep (default 7)
BACKUPS_TO_KEEP="7"
# Name of Screen session
SCRNAME="minecraft"
## The Java command to run the server, uncomment one INVOCATION line only!
# Nothing special, just start the server with 1Gb RAM
# INVOCATION="java -Xms1024M -Xmx1024M -Djava.net.preferIPv4Stack=true -jar $SERVICE nogui"
# This is what I run my server with, tune your RAM usage accordingly
# Tested fastest GC - Default parallel new gen collector, plus parallel old gen collector
INVOCATION="java -Xms1024M -Xmx1024M -Djava.net.preferIPv4Stack=true -XX:MaxPermSize=128M -XX:UseSSE=3 -XX:-DisableExplicitGC -XX:+UseParallelOldGC -XX:ParallelGCThreads=$CPU_COUNT -XX:+AggressiveOpts -XX:+UseCompressedStrings -jar $SERVICE nogui"
# I removed these "performance" commands as I don't see any difference with them
# -XX:+UseFastAccessorMethods -XX:+UseAdaptiveGCBoundary
# I've had a suggestion from a Java tuning engineer to use these
# -XX:+AggressiveOpts -XX:+UseCompressedStrings
# Add HugePage support if you have it configured on the OS
# You should be using HugePages if you give more than 4Gb on the Java invocation line
# -XX:+UseLargePages
SETTINGS_FILE_WORKING="1"
## End of settings file
## ---------------------------------------------------------
COMMENT
### ### ### ### ### ### ### ### ### ### ### ### ### ###
### You shouldn't need to edit anything below here! ###
### ### ### ### ### ### ### ### ### ### ### ### ### ###
## Apply the settings file
# define the location of the settings file from the name of this script
SETTINGSFILE="/etc/default/$(basename $0)"
# check the file exists, and fail if not
if [ ! -f "$SETTINGSFILE" ]
then
echo " * [ERROR] Settings file $SETTINGSFILE does not exist. Can't run!"
exit 1;
fi
# if exists so source it
. "$SETTINGSFILE"
# if the settings are not applying for some reason, then fail
if [ ! "$SETTINGS_FILE_WORKING" == "1" ]
then
echo " * [ERROR] Settings file $SETTINGSFILE is not applying. Can't run!"
echo " Check your options are uncommented and you haven't copied the <<COMMENT block."
exit 1;
fi
## Get some more info from the settings file
# Find the world name from the existing server file
WORLDNAME="$(grep -E 'level-name' $MCPATH/server.properties | sed -e s/.*level-name=//)"
# Find the port number from the existing server file
SERVERPORT="$(grep -E 'server-port' $MCPATH/server.properties | sed -e s/.*server-port=//)"
## Runs all commands as the non-root user
as_user() {
ME="$(whoami)"
if [ "$ME" == "$USERNAME" ]
then
bash -c "$1"
else
su - "$USERNAME" -c "$1"
fi
}
## Check if the server is running or not, and get the Java Process ID if it is
server_running() {
# Get the PID of the running Screen session:
# ps, remove grep, look for screen, look for the screen session $SCRNAME to differentiate between multiple servers, awk out everything but the pid
SCREENPID=""
SCREENPID="$(ps -ef | grep -v grep | grep -i screen | grep $SCRNAME | awk '{print $2}')"
# if the screen session with $SCRNAME is not running, then the server is not running, so we return 1 and exit the function
if [ -z "$SCREENPID" ]
then
return 1
fi
# PID="$(ps -ef | grep -v grep | grep -i screen | grep $SCRNAME | awk '{print $2}' | xargs ps -f --ppid | grep $SERVICE | awk '{print $2}')"
# use the screen session pid to get the parent pid, which is the actual pid of the java process, check that process is actually $SERVICE
JAVAPID="$(ps -f --ppid $SCREENPID | grep $SERVICE | awk '{print $2}')"
# if the java process is not running, then the server is not running, so we return 1 to exit the function
if [ -z "$JAVAPID" ]
then
return 1
fi
# if we haven't failed those two tests, we have a running server, so we return success
return 0
}
## Start the server executable as a service
mc_start() {
if server_running
then
echo " * [ERROR] $SERVERNAME was already running (pid $JAVAPID). Not starting!"
exit 1
else
echo " * $SERVERNAME was not already running. Starting..."
echo " * Using map named \"$WORLDNAME\"..."
as_user "cd \"$MCPATH\" && screen -c /dev/null -dmS $SCRNAME $INVOCATION"
echo " * Checking $SERVERNAME is running..."
# start a counter and check for 15 seconds if the server is running or not
COUNT=0
while [ $COUNT -lt 15 ]; do
if server_running
then
echo " * [OK] $SERVERNAME is now running (pid $JAVAPID)."
exit 0
else
let COUNT=COUNT+1
sleep 1
fi
done
# if we've reached here, the server hasn't started in 15 seconds
echo " * [ERROR] Could not start $SERVERNAME."
exit 1
fi
}
## Stop the executable
mc_stop() {
if server_running
then
echo " * $SERVERNAME is running (pid $JAVAPID). Commencing shutdown..."
echo " * Notifying users of shutdown..."
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"say SERVER SHUTTING DOWN IN 10 SECONDS. Saving map...\"\015'"
echo -n " * Saving map named \"$WORLDNAME\" to disk"
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"save-all\"\015'"
# start a counter and check for 20 seconds for when the world has saved
COUNT=0
while [ $COUNT -lt 10 ]; do
echo -n "."
sleep 2
if [ -f "$MCPATH/logs/latest.log" ]
then
if [[ "$(tail -n 20 $MCPATH/logs/latest.log | grep -E 'Save complete|Saved the world' | wc -l)" -gt 0 ]]; then
COUNT=99
fi
fi
let COUNT=COUNT+1
done
echo ""
echo -n " * Stopping $SERVERNAME"
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"stop\"\015'"
# start a counter and check for 15 seconds if the server is running or not
COUNT=0
while [ $COUNT -lt 15 ]; do
if server_running
then
echo -n "."
let COUNT=COUNT+1
sleep 1
else
echo ""
echo " * [OK] $SERVERNAME is shut down."
exit 0
fi
done
echo ""
# if we've reached here, the server hasn't stopped in 15 seconds
echo " * [ERROR] $SERVERNAME is still running (pid $JAVAPID). Could not be shutdown!"
exit 1
else
echo " * [OK] $SERVERNAME is shut down."
fi
}
## Set the server read-only, save the map, and have Linux sync filesystem buffers to disk
mc_saveoff() {
if server_running
then
echo " * $SERVERNAME is running. Commencing save..."
echo " * Notifying users of save..."
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"say SERVER BACKUP STARTING. Server going read-only...\"\015'"
echo " * Setting server read-only..."
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"save-off\"\015'"
echo " * Saving map named \"$WORLDNAME\" to disk..."
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"save-all\"\015'"
sync
sleep 10
echo " * [OK] Map saved."
else
echo " * [INFO] $SERVERNAME was not running. Not suspending saves."
fi
}
## Set the server read-write
mc_saveon() {
if server_running
then
echo " * $SERVERNAME is running. Re-enabling saves..."
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"say SERVER BACKUP ENDED. Server going read-write...\"\015'"
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"save-on\"\015'"
else
echo " * [INFO] $SERVERNAME was not running. Not resuming saves."
fi
}
## Checks for update, exits if update not required, updates if the server is not running
mc_updatecheck() {
echo " * Downloading latest $SERVERNAME executable..."
as_user "cd \"$MCPATH\" && curl -# -L -o \"$MCPATH/$SERVICE.update\" \"$SERVER_URL\""
if [ -f "$MCPATH/$SERVICE.update" ]
then
echo " * Checking downloaded update against existing server..."
if $(diff "$MCPATH/$SERVICE" "$MCPATH/$SERVICE.update" >/dev/null)
then
echo " * You are already running the latest version of $SERVERNAME!"
exit 0; # keep this exit in as we don't need to do anything
fi
else
echo " * [ERROR] $SERVERNAME update could not be downloaded."
exit 1
fi
if server_running
then
echo " * $SERVERNAME is running (pid $JAVAPID). Shutting down for update..."
else
as_user "/bin/cp \"$MCPATH/$SERVICE.update\" \"$MCPATH/$SERVICE\""
echo " * [OK] $SERVERNAME successfully updated."
fi
}
## Actually do the executable update
mc_updatedo() {
if server_running
then
echo " * [ERROR] $SERVICE is still running (pid $JAVAPID). Cannot update!"
exit 1
else
as_user "/bin/cp \"$MCPATH/$SERVICE.update\" \"$MCPATH/$SERVICE\""
echo " * [OK] $SERVERNAME successfully updated."
fi
}
## Check and see if a worldname was given to the backup command. Use the default world, or check the optional world exists and exit if it doesn't.
mc_checkbackup() {
if [ -n "$1" ]
then
WORLDNAME="$1"
if [ -d "$MCPATH/$WORLDNAME" ]
then
echo " * Found world named \"$MCPATH/$WORLDNAME\""
else
echo " * Could not find world named \"$MCPATH/$WORLDNAME\""
exit 1
fi
fi
}
## Backs up map by rsyncing current world to backup location, creates tar.gz with datestamp
mc_backupmap() {
echo " * Backing up $SERVERNAME map named \"$WORLDNAME\"..."
echo " * Syncing \"$MCPATH/$WORLDNAME\" to \"$BACKUPPATH/$WORLDNAME\""
as_user "rsync --checksum --group --human-readable --copy-links --owner --perms --recursive --times --update --delete \"$MCPATH/$WORLDNAME\" \"$BACKUPPATH\""
# if the nether exists, back it up
WORLDNETHER="$WORLDNAME""_nether"
if [ -d "$MCPATH/$WORLDNETHER" ]
then
echo " * Syncing \"$MCPATH/$WORLDNETHER\" to \"$BACKUPPATH/$WORLDNETHER\""
as_user "rsync --checksum --group --human-readable --copy-links --owner --perms --recursive --times --update --delete \"$MCPATH/$WORLDNETHER\" \"$BACKUPPATH\""
else
echo " * \"$MCPATH/$WORLDNETHER\" doesn't exist, skipping."
fi
# if the end exists, back it up
WORLDTHEEND="$WORLDNAME""_the_end"
if [ -d "$MCPATH/$WORLDTHEEND" ]
then
echo " * Syncing \"$MCPATH/$WORLDTHEEND\" to \"$BACKUPPATH/$WORLDTHEEND\""
as_user "rsync --checksum --group --human-readable --copy-links --owner --perms --recursive --times --update --delete \"$MCPATH/$WORLDTHEEND\" \"$BACKUPPATH\""
else
echo " * \"$MCPATH/$WORLDTHEEND\" doesn't exist, skipping."
fi
sleep 10
echo " * Creating compressed backup..."
NOW="$(date +%Y-%m-%d.%H-%M-%S)"
# Create a compressed backup file and background it so we can get back to restarting the server
# You can tell when the compression is done as it makes an md5sum file of the backup
as_user "cd "$BACKUPPATH" && tar cfz \""$WORLDNAME"_backup_"$NOW".tar.gz\" \"$WORLDNAME\" && md5sum \""$WORLDNAME"_backup_"$NOW".tar.gz\" > \""$WORLDNAME"_backup_"$NOW".tar.gz.md5\" &"
echo " * [OK] Backed up map \"$WORLDNAME\"."
# if we backed up the nether, create a backup of that too
if [ -d "$BACKUPPATH/$WORLDNETHER" ]
then
as_user "cd "$BACKUPPATH" && tar cfz \""$WORLDNETHER"_backup_"$NOW".tar.gz\" \"$WORLDNETHER\" && md5sum \""$WORLDNETHER"_backup_"$NOW".tar.gz\" > \""$WORLDNETHER"_backup_"$NOW".tar.gz.md5\" &"
echo " * [OK] Backed up map \"$WORLDNETHER\"."
fi
# if we backed up the end, create a backup of that too
if [ -d "$BACKUPPATH/$WORLDTHEEND" ]
then
as_user "cd "$BACKUPPATH" && tar cfz \""$WORLDTHEEND"_backup_"$NOW".tar.gz\" \"$WORLDTHEEND\" && md5sum \""$WORLDTHEEND"_backup_"$NOW".tar.gz\" > \""$WORLDTHEEND"_backup_"$NOW".tar.gz.md5\" &"
echo " * [OK] Backed up map \"$WORLDTHEEND\"."
fi
# we can safely background the above commands and get back to restarting the server
}
## Backs up executable by copying it to backup location
mc_backupexe() {
echo " * Backing up the $SERVERNAME server executable..."
NOW="$(date +%Y-%m-%d.%H-%M-%S)"
as_user "cp \""$MCPATH"/"$SERVICE"\" \""$BACKUPPATH"/"$SERVICE"_backup_"$NOW".jar\""
echo " * [OK] Backed up executable."
}
## Removes any backups older than $BACKUPS_TO_KEEP days, designed to be called by daily cron job
mc_removeoldbackups() {
echo " * Removing backups older than $BACKUPS_TO_KEEP days..."
as_user "cd \"$BACKUPPATH\" && find . -name \"*backup*\" -type f -mtime +$BACKUPS_TO_KEEP | xargs rm -fv"
echo " * Removed old backups."
}
## Rotates logfile to server.1 through server.7, designed to be called by daily cron job
mc_logrotate() {
# Define a function to copy the old logfile to the new
mc_copylog() {
as_user "/bin/cp $logfile $MCPATH/$LOGNEW"
}
# Server logfiles in chronological order
LOGLIST="$(ls -r $MCPATH/server.log* | grep -v lck)"
# How many logs to keep
COUNT="6"
# Look at all the logfiles
for logfile in $LOGLIST; do
LOGTMP="$(basename $logfile | cut -d '.' -f 3)"
# If we're working with server.log then append .1
if [ -z "$LOGTMP" ]
then
LOGNEW="server.log.1"
mc_copylog
# Otherwise, check if the file number is under $COUNT
elif [ "$LOGTMP" -gt "$COUNT" ]
then
# If so, delete it
as_user "rm -f $logfile"
else
# Otherwise, add one to the number
LOGBASE="$(basename $logfile | cut -d '.' -f 1-2)"
LOGNEW="$LOGBASE.$(($LOGTMP+1))"
mc_copylog
fi
done
# Blank the existing logfile to renew it
as_user "echo -n \"\" > $MCPATH/server.log"
}
## Check if server is running and display PID
mc_status() {
if server_running
then
echo " * $SERVERNAME status: Running (pid $JAVAPID)."
else
echo " * $SERVERNAME status: Not running."
exit 1
fi
}
## Display some extra environment informaton
mc_info() {
if server_running
then
RSS="$(ps --pid $JAVAPID --format rss | grep -v RSS)"
echo " - Java Path : $(readlink -f $(which java))"
echo " - Start Command : $INVOCATION"
echo " - Server Path : $MCPATH"
echo " - World Name : $WORLDNAME"
echo " - Process ID : $JAVAPID"
echo " - Screen Session : $SCRNAME"
echo " - Memory Usage : $((RSS/1024)) Mb ($RSS kb)"
# Check for HugePages support in kernel, display statistics if HugePages are available, otherwise skip
if [ -n "$(grep HugePages_Total /proc/meminfo | awk '{print $2}')" -a "$(grep HugePages_Total /proc/meminfo | awk '{print $2}')" ]
then
HP_SIZE="$(grep Hugepagesize /proc/meminfo | awk '{print $2}')"
HP_TOTAL="$(grep HugePages_Total /proc/meminfo | awk '{print $2}')"
HP_FREE="$(grep HugePages_Free /proc/meminfo | awk '{print $2}')"
HP_RSVD="$(grep HugePages_Rsvd /proc/meminfo | awk '{print $2}')"
HP_USED="$((HP_TOTAL-HP_FREE+HP_RSVD))"
TOTALMEM="$((RSS+(HP_USED*HP_SIZE)))"
echo " - HugePage Usage : $((HP_USED*(HP_SIZE/1024))) Mb ($HP_USED HugePages)"
echo " - Total Memory Usage : $((TOTALMEM/1024)) Mb ($TOTALMEM kb)"
fi
echo " - Active Connections : "
netstat --inet -tna | grep -E "Proto|$SERVERPORT"
else
echo " * $SERVERNAME is not running. Unable to give info."
exit 1
fi
}
## Connect to the active Screen session, disconnect with Ctrl+a then d
mc_console() {
if server_running
then
as_user "screen -S $SCRNAME -dr"
else
echo " * [ERROR] $SERVERNAME was not running! Unable to console."
exit 1
fi
}
## Broadcasts a message
mc_say() {
say_string="${@:1}"
if [[ -z "$say_string" ]]
then
echo " * You need to enter your message. Usage; \"minecraft say message\""
elif server_running
then
echo " * Broadcasting \"$say_string\""
as_user "screen -p 0 -S $SCRNAME -X eval 'stuff \"say $say_string\"\015'"
else
echo " * [ERROR] $SERVERNAME was not running!"
exit 1
fi
}
## These are the parameters passed to the script
case "$1" in
start)
mc_start
;;
stop)
mc_stop
;;
restart)
mc_stop
sleep 1
mc_start
;;
update)
mc_updatecheck
mc_stop
mc_backupmap
mc_backupexe
mc_updatedo
mc_start
;;
backup)
mc_checkbackup "$2"
mc_saveoff
mc_backupmap
mc_backupexe
mc_saveon
;;
status)
mc_status
;;
info)
mc_info
;;
console)
mc_console
;;
# These are intended for cron usage, not regular users.
removeoldbackups)
mc_removeoldbackups
;;
logrotate)
mc_logrotate
;;
save-off)
mc_saveoff
;;
save-on)
mc_saveon
;;
# Debug usage only
justbackup) # don't use this while the server is running!!!
mc_checkbackup
mc_backupmap
mc_backupexe
;;
say)
mc_say "${@:2}"
;;
*)
echo " * Usage: minecraft {start|stop|restart|backup (worldname)|save-on|save-off|update|status|info|console|say}"
exit 1
;;
esac
exit 0