From 598653a4cc89d5588315607a367a87083b73da7c Mon Sep 17 00:00:00 2001 From: Jonas Gunz Date: Sun, 4 Oct 2020 23:28:23 +0200 Subject: initial --- runner.sh | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100755 runner.sh (limited to 'runner.sh') diff --git a/runner.sh b/runner.sh new file mode 100755 index 0000000..1ec1fbc --- /dev/null +++ b/runner.sh @@ -0,0 +1,109 @@ +#!/bin/bash + +# Minecraft Java runner script + +function stop() { + running || exit 0 + test -p stdin || exit 1 + echo stop > stdin + + >&2 echo -n Waiting for server to stop + while kill -s 0 $PID > /dev/null 2>&1; do + sleep 1 + >&2 echo -n . + done + + >&2 echo Done! + + rm server.pid stdin + + exit 0 +} + +function start() { + running && fail "Server appears to already be running. PID: $PID" + + [ -z "$JVMARGS" ] && echo 'WARNING: $JVMARGS not set' + [ -e stdin -a ! -p stdin ] && rm stdin + + mkfifo stdin + + "$JAVA" $JVMARGS -jar "$MCJAR" -nogui $MCARGS \ + < <(while [ -p stdin ]; do timeout 10s cat stdin; done) \ + > /dev/null & + PID=$! + + echo $PID > server.pid + echo "Started server with PID $PID" + + exit 0 +} + +function attach() { + running || fail "Server is not running." + + echo "CTRL-D (EOF) to exit." + + tail -f logs/latest.log & + TAILPID=$! + + while read line; do + echo "$line" > stdin + done + + kill -s SIGKILL $TAILPID + + exit 0 +} + +function running() { + [ -e server.pid ] || return 1 + + PID=$(cat server.pid) + kill -s 0 $PID && return 0 + + rm server.pid stdin + return 1 +} > /dev/null 2>&1 + +function fail() { + >&2 echo $@ + exit 1 +} + +function stdin_redirector(){ + while [ -p stdin ]; do + cat stdin + done +} + +[ -e settings.sh ] && source settings.sh \ + || >&2 echo "WARNING: settings.sh not found." +# settings.sh +# JVMARGS, JAVA, MCARGS, MCJAR + +JAVA=${JAVA:-java} +MCJAR=${MCJAR:-server.jar} + +which "$JAVA" > /dev/null 2>&1 || fail "No java executable." + +case $1 in + start) + start;; + stop) + stop;; + status) + running || fail "Server is not running." + >&2 echo "Server is running" + exit 0;; + attach) + attach;; + command) + running || fail "Server is not running." + shift + echo $@ > stdin;; + *) + >&2 echo "Minecraft Java server runner" + >&2 echo "$0 start|stop|status|attach|command ..." + fail "Invalid argument $1" +esac -- cgit v1.2.3