You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.7 KiB
Python
62 lines
1.7 KiB
Python
import os
|
|
import sys
|
|
import socket
|
|
import time
|
|
import subprocess
|
|
|
|
CHECK_USERS_CMD = ["/root/mcrcon/mcrcon", "-H", "192.168.0.21", "-p", "password", "list"]
|
|
CHECK_INTERVAL = 60
|
|
WASTED_LIMIT = 20 * 60
|
|
|
|
def are_users_there():
|
|
try:
|
|
output = subprocess.check_output(CHECK_USERS_CMD)
|
|
# Maybe, server isn't running?
|
|
except subprocess.CalledProcessError as err:
|
|
print(f"Cannot access mcrcon: {err}", flush=True)
|
|
return True
|
|
header = output.decode().split("\n")[0]
|
|
if header.startswith("There are 0 of"):
|
|
return False
|
|
return True
|
|
|
|
def server_shutdown():
|
|
subprocess.run("service minecraft stop".split(" "))
|
|
|
|
def server_startup():
|
|
subprocess.run("service minecraft start".split(" "))
|
|
|
|
def main():
|
|
wasted_cnt_seconds = 0
|
|
while True:
|
|
if are_users_there():
|
|
wasted_cnt_seconds = 0
|
|
print("Users ok", flush=True)
|
|
time.sleep(CHECK_INTERVAL)
|
|
continue
|
|
|
|
wasted_cnt_seconds += CHECK_INTERVAL
|
|
print(f"No users ({wasted_cnt_seconds})", flush=True)
|
|
if wasted_cnt_seconds > WASTED_LIMIT:
|
|
print("Wasted limit", flush=True)
|
|
server_shutdown()
|
|
wait_client()
|
|
server_startup()
|
|
print("Server started", flush=True)
|
|
wasted_cnt_seconds = 0
|
|
|
|
time.sleep(CHECK_INTERVAL)
|
|
|
|
def wait_client():
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
sock.bind(("0.0.0.0", 25565))
|
|
print("Listening...", flush=True)
|
|
sock.listen(0)
|
|
connection, address = sock.accept()
|
|
print(f"Connection from: {address}", flush=True)
|
|
connection.close()
|
|
sock.close()
|
|
|
|
if __name__ == '__main__':
|
|
main()
|