launch.sh 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/bash
  2. # https://github.com/itzg/dockerfiles/blob/master/minecraft-server/start.sh
  3. set -e
  4. sed -i "/^minecraft/s/:1000:1000:/:${UID}:${GID}:/g" /etc/passwd
  5. sed -i "/^minecraft/s/:1000:/:${GID}:/g" /etc/group
  6. cd /home/minecraft
  7. if [ ! -e eula.txt ]; then
  8. echo "# Generated via Docker at $(date)" > eula.txt
  9. echo "eula=TRUE" >> eula.txt
  10. chown ${UID}:${GID} eula.txt
  11. fi
  12. if [ ! -e server.properties ]; then
  13. echo "Missing server.properties"
  14. exit 99
  15. fi
  16. # create ops.txt file with the names
  17. # There whitelist code https://github.com/itzg/dockerfiles/blob/master/minecraft-server/start-minecraft.sh#L540
  18. # but it doesn't work. See https://www.beastnode.com/portal/knowledgebase/6/Setting-Up-a-Whitelist-or-Make-Your-Server-Private.html
  19. # on using /whitelist add username (to actually setup and use the whitelist.json file) :P
  20. if [ ! -e banned-players.json ]; then
  21. echo '[]' > banned-players.json
  22. chown ${UID}:${GID} banned-players.json
  23. fi
  24. if [ ! -e banned-ips.json ]; then
  25. echo '[]' > banned-ips.json
  26. chown ${UID}:${GID} banned-ips.json
  27. fi
  28. EXTRA_ARGS=""
  29. # Optional disable console
  30. if [[ ${CONSOLE} = false || ${CONSOLE} = FALSE ]]; then
  31. EXTRA_ARGS+="--noconsole"
  32. fi
  33. # Optional disable GUI for headless servers
  34. if [[ ${GUI} = false || ${GUI} = FALSE ]]; then
  35. EXTRA_ARGS="${EXTRA_ARGS} nogui"
  36. fi
  37. # put these prior JVM_OPTS at the end to give any memory settings there higher precedence
  38. echo "Setting initial memory to ${INIT_MEMORY:-${MEMORY}} and max to ${MAX_MEMORY:-${MEMORY}}"
  39. JVM_OPTS="-Xms${INIT_MEMORY:-${MEMORY}} -Xmx${MAX_MEMORY:-${MEMORY}} ${JVM_OPTS}"
  40. echo "Switching to user 'minecraft' and launching $SERVER"
  41. # start in the backround
  42. su-exec minecraft java $JVM_XX_OPTS $JVM_OPTS $STARTUP -jar $SERVER "$@" $EXTRA_ARGS &
  43. MC_PID=$!
  44. echo "Minecraft running pid $MC_PID ..."
  45. RCON="rcon-cli --port 25575 --password mindcraft"
  46. save_shutdown() {
  47. echo "Received TERM..."
  48. echo "Warning users"
  49. $RCON say "Saving and Shutting down Minecraft now"
  50. sleep 1
  51. echo "Saving..."
  52. $RCON save-all
  53. sleep 1
  54. echo "Stop..."
  55. $RCON stop
  56. wait $MC_PID
  57. }
  58. # Ok, trap TERM
  59. trap save_shutdown TERM INT
  60. echo "Waiting for Minecraft ($MC_PID)..."
  61. wait $MC_PID
  62. echo "Minecraft is shutdown."