#!/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 } [ -e settings.sh ] && source settings.sh \ || >&2 echo "WARNING: settings.sh not found." # settings.sh # JVMARGS, JAVA, MCARGS, MCJAR, LOCK # TODO LOCK 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