Hi,
In order to save on my electricity bill, I would like to implement a script that automatically shuts off both the mc server and the physical machine itself after a 30 minute timer that starts when the last player has logged off. After searching around for a while on the topic, I have not found any applicable solution for a dedicated mc server hosting machine and would like to ask if anybody has already come across/faced this problem. I asked chat gpt to generate the following code but I am unsure as to how to implement it:
#!/bin/bash
# Set your MineOS server directory
MINEOS_DIR="/path/to/your/mineos/server"
# Function to check if players are online
check_players() {
# Logic to check if players are online (replace this with your actual check)
# Example: You might grep server logs for player activity
if grep -q "Player joined the game" "$MINEOS_DIR/logs/latest.log"; then
return 0 # Players are online
else
return 1 # No players online
fi
}
# Function to initiate shutdown
shutdown_server() {
echo "No players detected. Initiating shutdown process..."
# Command to shutdown your MineOS server
shutdown -h now
}
# Main loop
while true; do
if check_players; then
echo "Players are online."
sleep 300 # Check every 5 minutes
else
echo "No players detected."
# Start timer for 30 minutes
sleep 1800 # 30 minutes
# Check again if players are still not online
if ! check_players; then
shutdown_server
exit 0
fi
fi
done
Thanks,
Kristóf
PS. for those wondering, I plan on hosting a web server on something like a raspberry pi pico that would enable people to start the server when the dedicated machine is offline.