Automatic shutdown both mc server and machine

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:


# Set your MineOS server directory

# 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
        return 1 # No players online

# 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
        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
            exit 0


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.

Hi and good luck

In stead of relying on log-parsing, look inty opening you server for server-wuery in our

Thsi will allow you to simply ask the server how many players are online.
Take a look at athis github repo for how someone used this to create a shellbased minecraft server controll . GitHub - MinecraftSources/MinecraftServerControlScript: Powerful command-line control for UNIX and Linux powered Minecraft servers