Initial commit
This commit is contained in:
		
						commit
						933a5a06c9
					
				
							
								
								
									
										17
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							@ -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.
 | 
			
		||||
							
								
								
									
										25
									
								
								mineautoshutdown.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										25
									
								
								mineautoshutdown.sh
									
									
									
									
									
										Executable file
									
								
							@ -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
 | 
			
		||||
							
								
								
									
										47
									
								
								minecraft.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								minecraft.php
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
$DIGITALOCEAN_TOKEN="dop_your_digitalocean_token_here";
 | 
			
		||||
$DROPLET_ID="your_droplet_id_here";
 | 
			
		||||
$PASSWORD="your_password_here";
 | 
			
		||||
 | 
			
		||||
if ($_GET['password'] !== $PASSWORD) {
 | 
			
		||||
  usleep(rand(0,999999));
 | 
			
		||||
  exit("Invalid password.");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
if ($_GET['action'] == "on") {
 | 
			
		||||
 | 
			
		||||
$out = `curl -X POST \
 | 
			
		||||
  -H "Content-Type: application/json" \
 | 
			
		||||
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
 | 
			
		||||
  -d '{"type":"power_on"}' \
 | 
			
		||||
  "https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`;
 | 
			
		||||
 | 
			
		||||
} elseif ($_GET['action'] == "off") {
 | 
			
		||||
 | 
			
		||||
$out = `curl -X POST \
 | 
			
		||||
  -H "Content-Type: application/json" \
 | 
			
		||||
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
 | 
			
		||||
  -d '{"type":"power_off"}' \
 | 
			
		||||
  "https://api.digitalocean.com/v2/droplets/$DROPLET_ID/actions"`;
 | 
			
		||||
 | 
			
		||||
} elseif ($_GET['action'] == "status") {
 | 
			
		||||
 | 
			
		||||
$out = `curl -X GET \
 | 
			
		||||
  -H "Content-Type: application/json" \
 | 
			
		||||
  -H "Authorization: Bearer $DIGITALOCEAN_TOKEN" \
 | 
			
		||||
  "https://api.digitalocean.com/v2/droplets/$DROPLET_ID"`;
 | 
			
		||||
 | 
			
		||||
} else {
 | 
			
		||||
 | 
			
		||||
echo "Invalid action.";
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
$json = json_decode($out, true);
 | 
			
		||||
 | 
			
		||||
if ($json['action']) {
 | 
			
		||||
  echo $json['action']['status'];
 | 
			
		||||
} elseif ($json['droplet']) {
 | 
			
		||||
  echo $json['droplet']['status'];
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								minestatus.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								minestatus.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
from mcstatus import JavaServer
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
# You can pass the same address you'd enter into the address field in minecraft into the 'lookup' function
 | 
			
		||||
# If you know the host and port, you may skip this and use JavaServer("example.org", 1234)
 | 
			
		||||
server = JavaServer.lookup("your.minecraft.server.hostname.here")
 | 
			
		||||
 | 
			
		||||
# 'status' is supported by all Minecraft servers that are version 1.7 or higher.
 | 
			
		||||
# Don't expect the player list to always be complete, because many servers run
 | 
			
		||||
# plugins that hide this information or limit the number of players returned or even
 | 
			
		||||
# alter this list to contain fake players for purposes of having a custom message here.
 | 
			
		||||
status = server.status()
 | 
			
		||||
print(json.dumps({'players': status.players.online, 'ms': status.latency}))
 | 
			
		||||
#print(f"{{""players"": {status.players.online}, ""ms"": {status.latency} }}")
 | 
			
		||||
 | 
			
		||||
# 'ping' is supported by all Minecraft servers that are version 1.7 or higher.
 | 
			
		||||
# It is included in a 'status' call, but is also exposed separate if you do not require the additional info.
 | 
			
		||||
#latency = server.ping()
 | 
			
		||||
#print(f"The server replied in {latency} ms")
 | 
			
		||||
 | 
			
		||||
# 'query' has to be enabled in a server's server.properties file!
 | 
			
		||||
# It may give more information than a ping, such as a full player list or mod information.
 | 
			
		||||
#query = server.query()
 | 
			
		||||
#print(f"The server has the following players online: {', '.join(query.players.names)}")
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user