From 4b69568842afb262d30cca09e71784614820ac40 Mon Sep 17 00:00:00 2001 From: Chris Locker <5058266-chrisjoj@users.noreply.gitlab.com> Date: Fri, 2 Apr 2021 15:03:38 +0100 Subject: [PATCH 1/3] Handle SIGTERM by saving world and shutting down gracefully --- template/vanilla/Dockerfile.template | 2 +- template/vanilla/run-vanilla.sh | 39 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) 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 From 5ce396095b660248b6f2dd1339b3a67ede6256aa Mon Sep 17 00:00:00 2001 From: Chris Locker <5058266-chrisjoj@users.noreply.gitlab.com> Date: Mon, 5 Apr 2021 13:05:12 +0100 Subject: [PATCH 2/3] Handle SIGTERM by saving world and shutting down gracefully - alternative using screen --- template/vanilla/Dockerfile.template | 2 +- template/vanilla/run-vanilla.sh | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/template/vanilla/Dockerfile.template b/template/vanilla/Dockerfile.template index 2eac4de..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 procps && \ + 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 fcf93c9..74580a9 100755 --- a/template/vanilla/run-vanilla.sh +++ b/template/vanilla/run-vanilla.sh @@ -27,23 +27,19 @@ 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 say 'Server shutting down' > /vanilla/console + screen -p 0 -S terraria_server_screen -X eval "stuff 'say Server shutting down'\015" echo "Stopping Terraria server..." echo "Saving world" - echo exit > /vanilla/console + screen -p 0 -S terraria_server_screen -X eval "stuff 'exit'\015" + # echo exit > /vanilla/console # Waiting for server to finish saving & shutting down pid=$(pgrep -f ^./TerrariaServer) @@ -57,7 +53,10 @@ function graceful_shutdown() { } echo "Starting container, CMD: $CMD $@" -(tail -f > /vanilla/console & $CMD $@ < console) & +screen -AmdS terraria_server_screen bash -c "$CMD $@ | tee -a /config/server.log" +sleep 5 +tail -f /config/server.log & +#(tail -f > /vanilla/console & $CMD $@ < console) & tail -f /dev/null & wait ${!} From 313b9371592a22efe6fb6ddf215c2769bd1960f0 Mon Sep 17 00:00:00 2001 From: Chris Locker <5058266-chrisjoj@users.noreply.gitlab.com> Date: Mon, 5 Apr 2021 13:05:12 +0100 Subject: [PATCH 3/3] Handle SIGTERM by saving world and shutting down gracefully - alternative using screen --- template/vanilla/run-vanilla.sh | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/template/vanilla/run-vanilla.sh b/template/vanilla/run-vanilla.sh index 74580a9..f9d8f63 100755 --- a/template/vanilla/run-vanilla.sh +++ b/template/vanilla/run-vanilla.sh @@ -32,14 +32,13 @@ 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 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" - # echo exit > /vanilla/console # Waiting for server to finish saving & shutting down pid=$(pgrep -f ^./TerrariaServer) @@ -56,7 +55,6 @@ echo "Starting container, CMD: $CMD $@" screen -AmdS terraria_server_screen bash -c "$CMD $@ | tee -a /config/server.log" sleep 5 tail -f /config/server.log & -#(tail -f > /vanilla/console & $CMD $@ < console) & tail -f /dev/null & wait ${!} @@ -64,4 +62,4 @@ 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 +done