diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2020-10-04 23:28:23 +0200 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2020-10-04 23:28:23 +0200 |
commit | 598653a4cc89d5588315607a367a87083b73da7c (patch) | |
tree | 10540c1e9085e3fcf69dbf47ae2a7b457bda5cf5 /runner.sh | |
download | minecraft-isekai-598653a4cc89d5588315607a367a87083b73da7c.tar.gz |
initial
Diffstat (limited to 'runner.sh')
-rwxr-xr-x | runner.sh | 109 |
1 files changed, 109 insertions, 0 deletions
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 |