Skip to content

Instantly share code, notes, and snippets.

@jbrazio
Created May 30, 2020 14:50
Show Gist options
  • Save jbrazio/a47c72caeb19aac12e765ff19bc1cd7b to your computer and use it in GitHub Desktop.
Save jbrazio/a47c72caeb19aac12e765ff19bc1cd7b to your computer and use it in GitHub Desktop.
[OpenRC] Minecraft server
# /etc/conf.d/minecraft
#
# Minecraft - OpenRC scripts
# Copyright (C) 2017-2019 João Brázio [joao@brazio.org]
#
# For 1GB machines
#MINHEAP=128M
#MAXHEAP=1024M
# https://aikar.co/2018/07/02/tuning-the-jvm-g1gc-garbage-collector-flags-for-minecraft/
#CUSTOMARGS="-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1"
#!/sbin/openrc-run
# /etc/init.d/minecraft
#
# Minecraft - OpenRC scripts
# Copyright (C) 2017-2019 João Brázio [joao@brazio.org]
#
# Expects the game folder to be located at /srv/minecraft/<instance>
# Don't use directly the /etc/init.d/minecraft script, create a symlink for you instance.
#
# This is an example for Paper:
# - mkdir -p /srv/minecraft/paper
# - Download paper jar and place it there
# - ln -s /etc/init.d/minecraft /etc/init.d/minecraft.paper
# - cp /etc/conf.d/minecraft /etc/conf.d/minecraft.paper
#
# Dependencies:
# - Java VM at /usr/bin/java
# - mcrcon [https://github.com/Tiiffi/mcrcon]
#
USER=nobody
GROUP=nogroup
INSTANCE=${RC_SVCNAME##*.}
BASE="/srv/minecraft/${INSTANCE}"
PIDFILE="/var/run/${RC_SVCNAME}.pid"
RCON="/srv/minecraft/tools/mcrcon/mcrcon"
BIN="$(ls ${BASE} -v 2>/dev/null | grep -i "craftbukkit.*jar\\|spigot.*jar\\|paper*.*jar\\|minecraft_server.*jar" | head -n 1)"
config_get () {
echo $(grep ${1} "${BASE}/server.properties" | cut -d '=' -f 2)
}
depend () {
need net
after ipsec
}
start_pre () {
if [ -z "${BIN}" ]; then
eerror "${RC_SVCNAME} cannot find a proper server jar."
eerror "Check your game installation at ${BASE}."
return 1
fi
[ -f "${PIDFILE}" ] && rm ${PIDFILE}
return 0
}
start () {
ebegin "Starting Minecraft server ${INSTANCE}"
start-stop-daemon --start --chdir ${BASE} --pidfile ${PIDFILE} --make-pidfile \
--exec /usr/bin/java --user ${USER} --group ${GROUP} --background -- -server -Xms${MINHEAP:-128M} -Xmx${MAXHEAP:-1024M} ${CUSTOMARGS} -jar ${BIN} nogui
eend $?
}
stop_pre () {
if [ ! -f "${BASE}/server.properties" ]; then
eerror "${RC_SVCNAME} cannot find a proper server config file."
eerror "Check your game installation at ${BASE}."
return 1
fi
return 0
}
stop () {
ebegin "Stopping Minecraft server ${INSTANCE}"
if [ "$(config_get enable-rcon)" = "true" ]; then
# Fetch rcon configuration
RCON_SRV=$(config_get server.ip)
RCON_PORT=$(config_get rcon.port)
RCON_PASS=$(config_get rcon.password)
# Try to soft kill the server
${RCON} -H ${RCON_SRV:-localhost} -P ${RCON_PORT} -p ${RCON_PASS} save-all 1>/dev/null 2>&1
${RCON} -H ${RCON_SRV:-localhost} -P ${RCON_PORT} -p ${RCON_PASS} stop 1>/dev/null 2>&1
i=0
PID=$(cat ${PIDFILE})
while [ "${i}" -le 30 ]; do
kill -0 ${PID} 2>/dev/null && sleep 1 || break
i=$(( i + 1 ))
done
fi
# Hard stop the server
start-stop-daemon --stop --chdir ${BASE} --pidfile ${PIDFILE} --user ${USER} --group ${GROUP} --retry 5 2>/dev/null
eend 0
}
status () {
if [ "$(config_get enable-rcon)" = "true" ]; then
# Fetch rcon configuration
RCON_SRV=$(config_get server.ip)
RCON_PORT=$(config_get rcon.port)
RCON_PASS=$(config_get rcon.password)
${RCON} -H ${RCON_SRV:-localhost} -P ${RCON_PORT} -p ${RCON_PASS} version
${RCON} -H ${RCON_SRV:-localhost} -P ${RCON_PORT} -p ${RCON_PASS} tps
fi
}
#!/bin/sh
# /usr/bin/mcbackup
#
# Minecraft - Backup tool
# Copyright (C) 2017-2019 João Brázio [joao@brazio.org]
#
# Dependencies:
# - mcrcon [https://github.com/Tiiffi/mcrcon]
#
RCON="/srv/minecraft/tools/mcrcon/mcrcon"
BASE=/srv/minecraft
FLAVOUR=${1}
config_get () {
echo $(grep ${1} "${BASE}/${FLAVOUR}/server.properties" | cut -d '=' -f 2)
}
# No argument provided
[ -z "${FLAVOUR}" ] && exit 2
# Non existing flavour
[ -d "${BASE}/${FLAVOUR}" ] || exit 3
# Non exiting config file
[ -f "${BASE}/${FLAVOUR}/server.properties" ] || exit 4
# Create backup folder if not found
[ -d "${BASE}/${FLAVOUR}/backup" ] || mkdir "${BASE}/${FLAVOUR}/backup"
# Remote console not enabled
[ "$(config_get enable-rcon)" = "true" ] || exit 5
RCON_SRV=$(config_get server.ip)
RCON_PORT=$(config_get rcon.port)
RCON_PASS=$(config_get rcon.password)
# Pause world saving
${RCON} -H ${RCON_SRV} -P ${RCON_PORT} -p ${RCON_PASS} save-all
${RCON} -H ${RCON_SRV} -P ${RCON_PORT} -p ${RCON_PASS} save-off
# Create tarball
tar --exclude="backup/*" \
-cvjf "${BASE}/${FLAVOUR}/backup/mc-${FLAVOUR}-$(date +'%Y%m%d-%H%M%S').tbz2" -C "${BASE}" "${FLAVOUR}"
# Resume world saving
${RCON} -H ${RCON_SRV} -P ${RCON_PORT} -p ${RCON_PASS} save-on
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment