85 lines
2.7 KiB
Bash
Executable file
85 lines
2.7 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
|
|
# Minecraft server start.sh
|
|
# for use in slapping in tmux and forgetting
|
|
# also rolls logs
|
|
|
|
# --- CONFIGURATION
|
|
|
|
# you can source an environment file like this:
|
|
#. .env
|
|
|
|
# auto-restart
|
|
AUTO_RESTART=${AUTO_RESTART:-1} # 1 to enable
|
|
RESTART_DELAY=${RESTART_DELAY:-3} # seconds
|
|
DONT_RESTART_IF=${DONT_RESTART_IF:0} # exit code
|
|
|
|
# java options
|
|
JAVA_BIN=${JAVA_BIN:-java} # full path to the java binary
|
|
#JVM_FLAGS= # Java flags
|
|
#NO_DEFAULT_JVM_FLAGS= # 1 to disable default flags
|
|
MEMORY_MIN=${MEMORY_MIN-4G} # Xms
|
|
MEMORY_MAX=${MEMORY_MAX:-4G} # Xmx
|
|
GC=${GC:-ZGC} # ShenandoahGC or ZGC
|
|
GC_PAR_THREADS=${GC_PAR_THREADS:-6} # "parallel" (runs while workers are stopped) thread count
|
|
# recommended to set to just below total thread count
|
|
GC_CONC_THREADS=${GC_CONC_THREADS:-"$(( half=GC_PAR_THREADS / 2, half == 0 ? 1 : half ))"}
|
|
# marker thread count, defaults to half of parallel
|
|
|
|
# mc options
|
|
MC_FLAGS=${MC_FLAGS:nogui} # Minecraft flags
|
|
SERVER_JAR=${SERVER_JAR:-server.jar} # server JAR file
|
|
|
|
# ---
|
|
|
|
gcs=(
|
|
[zgc]="-XX:+UseZGC"
|
|
[shenandoahgc]="-XX:+UseShenandoahGC"
|
|
)
|
|
|
|
java_args=()
|
|
|
|
[[ "${MEMORY_MIN:+x}" == 'x' ]] && java_args+=(-Xms"${MEMORY_MIN}")
|
|
java_args+=(-Xmx"${MEMORY_MAX}")
|
|
|
|
(( ! NO_DEFAULT_JVM_FLAGS )) && java_args+=(-XX:+ParallelRefProcEnabled -XX:+DisableExplicitGC -XX:+PerfDisableSharedMem -XX:MaxGCPauseMillis=10)
|
|
if [[ "${MEMORY_MIN,,}" == "${MEMORY_MAX,,}" ]]; then
|
|
java_args+=(-XX:+AlwaysPreTouch)
|
|
[[ "${GC,,}" == 'zgc' ]] && java_args+=(-XX:-ZUncommit)
|
|
fi
|
|
if (( $(</proc/sys/vm/nr_hugepages) > 0 )); then
|
|
java_args+=(-XX:+UseLargePages)
|
|
[[ $(</sys/kernel/mm/transparent_hugepage/enabled) != 'never' ]] && java_args+=(-XX:+UseTransparentHugePages)
|
|
fi
|
|
|
|
java_args+=("${gcs[${GC,,}]}")
|
|
[[ "${GC_CONC_THREADS:+x}" == 'x' ]] && java_args+=(-XX:ConcGCThreads="${GC_CONC_THREADS}")
|
|
java_args+=(-XX:ParallelGCThreads="${GC_PAR_THREADS}")
|
|
|
|
# shellcheck disable=SC2206
|
|
java_args+=(${JVM_FLAGS})
|
|
# shellcheck disable=SC2206
|
|
java_args+=(-jar "${SERVER_JAR}" $MC_FLAGS)
|
|
|
|
{
|
|
echo "Using settings:"
|
|
echo "Java binary: ${JAVA_BIN?}"
|
|
# shellcheck disable=SC2145
|
|
echo "Options: ${java_args[@]@Q} ${}"
|
|
echo "Auto restart: $( (( AUTO_RESTART )) && echo 'enabled' || echo 'disabled' )"
|
|
} 2>&1
|
|
|
|
|
|
while true; do
|
|
"${JAVA_BIN}" "${java_args[@]}" 2>&1 | tee ./"$(date -I)".log
|
|
exitcode=$?
|
|
|
|
echo "Process exited with code "$'\033'"[3$(( exitcode == 0 ? 2 : 1 ))m${exitcode}"$'\033'"[0m" >&2
|
|
|
|
#shellcheck disable=SC2076
|
|
(( AUTO_RESTART != 1 || DONT_RESTART_IF == exitcode )) &&
|
|
exit "${exitcode}"
|
|
|
|
echo "Restarting after ${RESTART_DELAY} seconds" >&2
|
|
sleep "${RESTART_DELAY?}"
|
|
done
|