This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stop the server gracefully when SIGTERM is sent (as docker stop does)
`docker stop` sends the `SIGTERM` signal to the process to stop it, but the Minecraft server process handles this by quitting immediately, not saving the world first. This differs from when the `stop` Minecraft command is used, as that will save the world first before quitting. This change adds a script (`start.sh`) which catches the `SIGTERM` signal and handles it by sending the `stop` command to the Minecraft server. As a bonus, `sendcommand` is now able to send commands to the server directly (by using a named pipe) rather than using RCON.
- Loading branch information
Showing
5 changed files
with
78 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/sh | ||
|
||
if [ $# -lt 2 ]; then | ||
echo "Usage: rcon <password> <commands>" | ||
exit 1 | ||
fi | ||
|
||
java -jar /mc/minecraft-rcon-client.jar 127.0.0.1:25575 "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
#!/bin/sh | ||
|
||
if [ $# -lt 2 ]; then | ||
echo "Usage: sendcommand <password> <commands>" | ||
if [ $# -lt 1 ]; then | ||
echo "Usage: sendcommand <command>" | ||
exit 1 | ||
fi | ||
|
||
java -jar /mc/minecraft-rcon-client.jar 127.0.0.1:25575 "$@" | ||
echo $* > /minecraft_stdin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#!/bin/sh | ||
|
||
_stop_server() { | ||
echo Received SIGTERM, stopping | ||
echo stop > /minecraft_stdin | ||
wait ${JAVA_PID} | ||
exit | ||
} | ||
|
||
mkfifo /minecraft_stdin | ||
trap "_stop_server" TERM | ||
|
||
cd /world | ||
|
||
java -Xms${JAVA_MIN_MEMORY} -Xmx${JAVA_MAX_MEMORY} -jar /mc/minecraft_server.jar nogui < /minecraft_stdin & | ||
JAVA_PID=$! | ||
|
||
# See: https://unix.stackexchange.com/questions/72962/getting-stdin-from-a-named-pipe | ||
exec 3> /minecraft_stdin | ||
|
||
# See: https://veithen.github.io/2014/11/16/sigterm-propagation.html | ||
wait ${JAVA_PID} | ||
trap - TERM INT | ||
wait ${JAVA_PID} | ||
|
||
exec 3>&- |