diff --git a/template/vanilla/Dockerfile.template b/template/vanilla/Dockerfile.template index 427a9d7..0314146 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 screen && \ 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..f9d8f63 100755 --- a/template/vanilla/run-vanilla.sh +++ b/template/vanilla/run-vanilla.sh @@ -27,5 +27,39 @@ if [ "${world:-null}" != null ]; then CMD="$CMD -world /config/$world" 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 + screen -p 0 -S terraria_server_screen -X eval "stuff 'say Server shutting down'\015" + + echo "Stopping Terraria server..." + echo "Saving world" + + # Send 'exit' command to Terraria Server + screen -p 0 -S terraria_server_screen -X eval "stuff 'exit'\015" + + # 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 $@ +screen -AmdS terraria_server_screen bash -c "$CMD $@ | tee -a /config/server.log" +sleep 5 +tail -f /config/server.log & + +tail -f /dev/null & wait ${!} + +pid=$(pgrep -f ^./TerrariaServer) +if [ -z "$pid" ]; then exit 1; fi +while [ -e /proc/$pid ]; do + sleep 5 +done