Skip to content

Commit 4b69568

Browse files
author
Chris Locker
committed
Handle SIGTERM by saving world and shutting down gracefully
1 parent 8d7b6b8 commit 4b69568

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

template/vanilla/Dockerfile.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ FROM mono:slim
33
# Update and install needed utils
44
RUN apt-get update && \
55
apt-get upgrade -y && \
6-
apt-get install -y curl nuget vim zip && \
6+
apt-get install -y curl nuget vim zip procps && \
77
apt-get clean && \
88
rm -rf /var/lib/apt/lists/*
99

template/vanilla/run-vanilla.sh

+38-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,42 @@ if [ "${world:-null}" != null ]; then
2727
CMD="$CMD -world /config/$world"
2828
fi
2929

30+
# Create named pipe if one doesn't already exist
31+
# This is so we can send input to TerrariaServer
32+
if [ ! -p "/vanilla/console" ]; then
33+
mkfifo /vanilla/console
34+
fi
35+
36+
# trap SIGTERM signal and call graceful_shutdown
37+
trap 'kill ${!}; graceful_shutdown' SIGTERM
38+
39+
function graceful_shutdown() {
40+
# Send a message to players that the server is shutting down
41+
echo say 'Server shutting down' > /vanilla/console
42+
43+
echo "Stopping Terraria server..."
44+
echo "Saving world"
45+
46+
echo exit > /vanilla/console
47+
48+
# Waiting for server to finish saving & shutting down
49+
pid=$(pgrep -f ^./TerrariaServer)
50+
if [ -z "$pid" ]; then exit 1; fi
51+
while [ -e /proc/$pid ]; do
52+
sleep 1
53+
done
54+
echo "World save complete"
55+
echo "Shutting down"
56+
exit 0
57+
}
58+
3059
echo "Starting container, CMD: $CMD $@"
31-
exec $CMD $@
60+
(tail -f > /vanilla/console & $CMD $@ < console) &
61+
62+
tail -f /dev/null & wait ${!}
63+
64+
pid=$(pgrep -f ^./TerrariaServer)
65+
if [ -z "$pid" ]; then exit 1; fi
66+
while [ -e /proc/$pid ]; do
67+
sleep 5
68+
done

0 commit comments

Comments
 (0)