commit 933a5a06c9ae8cf0333cdc95154536fb758c894b Author: Will Bradley Date: Thu Jan 26 23:15:40 2023 +0000 Initial commit 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 @@ +