From 933a5a06c9ae8cf0333cdc95154536fb758c894b Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Thu, 26 Jan 2023 23:15:40 +0000 Subject: [PATCH] Initial commit --- README.md | 17 ++++++++++++++++ mineautoshutdown.sh | 25 ++++++++++++++++++++++++ minecraft.php | 47 +++++++++++++++++++++++++++++++++++++++++++++ minestatus.py | 24 +++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100644 README.md create mode 100755 mineautoshutdown.sh create mode 100644 minecraft.php create mode 100644 minestatus.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..94fe40e --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Minecraft server automatic startup and shutdown + +Install the .php file on a webserver and configure the variables as desired. + +Place the remaining files on a server with CRON (could be the same server, just +not inside the web folder) and run the shell script every 5 minutes: + +`*/5 * * * * * /your/path/here/minecraftautoshutdown.sh` + +Ensure the same password is present in the php script and the shutdown script. + +Call the php script via HTTP using the correct password when players want to play: + +`curl http://your.webserver.here/minecraft.php?action=on&password=your_password_here` + +If the CRON job runs properly and has all the right variables/permissions/dependencies, +it will shut down the DO droplet about 30-40 minutes after no players are detected in the game. diff --git a/mineautoshutdown.sh b/mineautoshutdown.sh new file mode 100755 index 0000000..b3bce36 --- /dev/null +++ b/mineautoshutdown.sh @@ -0,0 +1,25 @@ +DO_STATUS_URL="http://example.com/minecraft.php" +status=$(curl -s "$DO_STATUS_URL?action=status") + +if [[ $status == "active" ]]; then + players=$(python3 minestatus.py | jq ".players") + + if [[ $players == 0 ]]; then + + count=$(cat /tmp/minecount) + + if [[ $count -gt 6 ]]; then + echo "players $players count $count, killing" + curl -s "$DO_STATUS_URL?action=off" + echo "0" > /tmp/minecount + else + echo $(($count+1)) > /tmp/minecount + echo "players $players count $count, waiting" + fi + else + echo "players $players, resetting" + echo "0" > /tmp/minecount + fi +else + echo "not active" +fi diff --git a/minecraft.php b/minecraft.php new file mode 100644 index 0000000..936502d --- /dev/null +++ b/minecraft.php @@ -0,0 +1,47 @@ +