Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative - Handle SIGTERM by saving world and shutting down gracefully #31

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion template/vanilla/Dockerfile.template
Original file line number Diff line number Diff line change
Expand Up @@ -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/*

Expand Down
36 changes: 35 additions & 1 deletion template/vanilla/run-vanilla.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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