Skip to content

Commit 199f125

Browse files
committed
add shutdown if no players online capability
1 parent 2f05dd1 commit 199f125

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

Dockerfile

+4-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ RUN apk update &&\
2929
apk add --no-cache procps tmux
3030
RUN ln -s ${HOME}/.local/share/Terraria/ /terraria
3131
COPY inject.sh /usr/local/bin/inject
32+
COPY handle-idle.sh /usr/local/bin/handle-idle
3233

3334
EXPOSE 7777
3435
ENV TMOD_SHUTDOWN_MSG="Shutting down!"
3536
ENV TMOD_AUTOSAVE_INTERVAL="*/10 * * * *"
37+
ENV TMOD_IDLE_CHECK_INTERVAL=""
38+
ENV TMOD_IDLE_CHECK_OFFSET=0
3639

3740
COPY config.txt entrypoint.sh ./
38-
RUN chmod +x entrypoint.sh /usr/local/bin/inject
41+
RUN chmod +x entrypoint.sh /usr/local/bin/inject /usr/local/bin/handle-idle
3942

4043
ENTRYPOINT [ "/terraria-server/entrypoint.sh" ]

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ You can inject [commands] from the host machine though. For example, assuming yo
4040

4141
# Environment Variables
4242

43-
Name | Default Value | |
44-
-----------------------|----------------|-
45-
TMOD_SHUTDOWN_MSG | Shutting Down! | Message that appears when server is shutting down
46-
TMOD_AUTOSAVE_INTERVAL | */10 * * * * | Cron expression that specifies how often to save the world. Default is every 10 minutes.
47-
48-
docker run -d --name tmod -e TMOD_SHUTDOWN_MSG="Goodbye" -e TMOD_AUTOSAVE_INTERVAL="*/15 * * * *" -p 7777:7777 -v /etc/localtime:/etc/localtime:ro rfvgyhn/tmodloader
43+
Name | Default Value | |
44+
-------------------------|----------------|-
45+
TMOD_SHUTDOWN_MSG | Shutting Down! | Message that appears when server is shutting down
46+
TMOD_AUTOSAVE_INTERVAL | */10 * * * * | Cron expression that specifies how often to save the world. Default is every 10 minutes.
47+
TMOD_IDLE_CHECK_INTERVAL | Disabled | Cron expression that specifies how often to check if no players are online. If none are online, the server will save the world and exit. This can be useful if your server costs are based on CPU usage.
48+
TMOD_IDLE_CHECK_OFFSET | 0 | This allows for sub-second resolution if the idle check interval is specified
49+
docker run -d --name tmod -e TMOD_SHUTDOWN_MSG="Goodbye" -e TMOD_AUTOSAVE_INTERVAL="*/15 * * * *" -e TMOD_IDLE_CHECK_INTERVAL="*/1 * * * *" -e TMOD_IDLE_CHECK_OFFSET=10 -p 7777:7777 -v /etc/localtime:/etc/localtime:ro rfvgyhn/tmodloader
4950

5051
# Sample Docker Compose
5152

entrypoint.sh

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/bin/sh
22

33
pipe=/tmp/tmod.out
4+
players=/tmp/tmod.players.out
45

56
function shutdown() {
67
[ -n "$TMOD_SHUTDOWN_MSG" ] && inject "say $TMOD_SHUTDOWN_MSG"
78
inject "exit"
8-
99
tmuxPid=$(pgrep tmux)
1010
tmodPid=$(pgrep --parent $tmuxPid Main)
1111
while [ -e /proc/$tmodPid ]; do
@@ -22,13 +22,17 @@ else
2222
trap shutdown SIGTERM SIGINT
2323

2424
saveMsg='Autosave - $(date +"%Y-%m-%d %T")'
25+
idleMsg='Idle Check - $(date +"%Y-%m-%d %T")'
2526

26-
if ! crontab -l | grep -q "Autosave"; then
27+
if [ ! -z "$TMOD_AUTOSAVE_INTERVAL" ] && ! crontab -l | grep -q "Autosave"; then
2728
(crontab -l 2>/dev/null; echo "$TMOD_AUTOSAVE_INTERVAL echo \"$saveMsg\" > $pipe && inject save") | crontab -
2829
fi
30+
if [ ! -z "$TMOD_IDLE_CHECK_INTERVAL" ] && ! crontab -l | grep -q "Idle Check"; then
31+
(crontab -l 2>/dev/null; echo "$TMOD_IDLE_CHECK_INTERVAL echo \"$idleMsg\" > $pipe && handle-idle $players") | crontab -
32+
fi
2933
mkfifo $pipe
30-
tmux new-session -d "$server -config config.txt > $pipe" &
31-
/usr/sbin/crond -d 8
34+
tmux new-session -d "$server -config config.txt | tee $pipe $players" &
35+
sleep 60 && /usr/sbin/crond -d 8 &
3236
cat $pipe &
3337

3438
wait ${!}

handle-idle.sh

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
3+
players="$1"
4+
5+
sleep $TMOD_IDLE_CHECK_OFFSET
6+
7+
echo "" > "$players"
8+
inject playing && sleep .2
9+
if ! grep -q "[1-9]" "$players"; then
10+
inject "exit"
11+
fi

inject.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#!/bin/sh
22

3-
tmux send-keys "$1" Enter
3+
# If no worlds exist, don't bother sending any commands
4+
ls /terraria/ModLoader/Worlds/*.wld >/dev/null || exit
5+
6+
tmux send-keys "$1" Enter

0 commit comments

Comments
 (0)