Search code examples
bashminecraftshutdown

Shutting down computer when no one is on servers


I run multiple minecraft servers on my tower and am trying to figure out a script to run in the background that will check if no one has been on any of the servers for the past 30 minutes and then shut down the servers and computer. Then I will use my raspberry pi for wake on lan when someone wants to join.

I figured out how to shut down the servers using rcon and the wake on lan stuff. The main problem I have is figuring out the shutdown. The only thing I've been able to find is how to check if someone is on that specific IP using

lsof -iTCP:25565 -sTCP:ESTABLISHED

But I don't know where to go from there. I am using a Linux machine and most of my experience with bash files comes from setting up the servers and that's about it. Any help you could give would be greatly appreciated! Thanks!


Solution

  • something like this then:

    #!/bin/bash
    
    PORTLIST="25565 25566 25567"
    TIMEOUT=3600
    
    let T=0
    declare -i START
    declare -i NOW
    START=$(date +%s)
    
    while [ $T -lt $TIMEOUT ] ; do
       sleep 30
       for PORT in $PORTLIST ; do
          if lsof -iTCP:$PORT -sTCP:ESTABLISHED >/dev/null ; then
             T=0
             START=$(date +%s)
             # starting over
             continue 2
             fi
          done
       NOW=$(date +%s)
       let T=NOW-START
       done
    echo timeout
    

    PORTLIST and TIMEOUT being the only variables to change