diff --git a/template/vanilla/Dockerfile.template b/template/vanilla/Dockerfile.template index 427a9d7..2eac4de 100644 --- a/template/vanilla/Dockerfile.template +++ b/template/vanilla/Dockerfile.template @@ -3,7 +3,7 @@ FROM mono:slim # Update and install needed utils RUN apt-get update && \ apt-get upgrade -y && \ - apt-get install -y curl nuget vim zip && \ + apt-get install -y curl nuget vim zip procps && \ apt-get clean && \ rm -rf /var/lib/apt/lists/* diff --git a/template/vanilla/run-vanilla.sh b/template/vanilla/run-vanilla.sh index baffe45..fcf93c9 100755 --- a/template/vanilla/run-vanilla.sh +++ b/template/vanilla/run-vanilla.sh @@ -27,5 +27,42 @@ if [ "${world:-null}" != null ]; then CMD="$CMD -world /config/$world" fi +# Create named pipe if one doesn't already exist +# This is so we can send input to TerrariaServer +if [ ! -p "/vanilla/console" ]; then + mkfifo /vanilla/console +fi + +# trap SIGTERM signal and call graceful_shutdown +trap 'kill ${!}; graceful_shutdown' SIGTERM + +function graceful_shutdown() { + # Send a message to players that the server is shutting down + echo say 'Server shutting down' > /vanilla/console + + echo "Stopping Terraria server..." + echo "Saving world" + + echo exit > /vanilla/console + + # Waiting for server to finish saving & shutting down + pid=$(pgrep -f ^./TerrariaServer) + if [ -z "$pid" ]; then exit 1; fi + while [ -e /proc/$pid ]; do + sleep 1 + done + echo "World save complete" + echo "Shutting down" + exit 0 +} + echo "Starting container, CMD: $CMD $@" -exec $CMD $@ +(tail -f > /vanilla/console & $CMD $@ < console) & + +tail -f /dev/null & wait ${!} + +pid=$(pgrep -f ^./TerrariaServer) +if [ -z "$pid" ]; then exit 1; fi +while [ -e /proc/$pid ]; do + sleep 5 +done \ No newline at end of file