Skip to content

Instantly share code, notes, and snippets.

@Owez
Last active August 2, 2021 15:36
Show Gist options
  • Save Owez/a92c65fc14caa8a6745db87f11ec6002 to your computer and use it in GitHub Desktop.
Save Owez/a92c65fc14caa8a6745db87f11ec6002 to your computer and use it in GitHub Desktop.
Automatic minecraft server runner used and maintained for myself; see constants for configuration. Dynamically uses spigot/paper jars placed in same dir
"""Automatic minecraft server runner used and maintained for myself; see constants for configuration. Dynamically uses spigot/paper jars placed in same dir"""
from __future__ import print_function
import os
import sys
import time
import signal
"""The possible beginnings of filenames for server jars"""
STARTJAR = ["spigot-", "paper-"]
"""Default ram to provide in MB if none is provided in args"""
DEFAULT_RAM_MB = 3072
"""Seconds to wait to restart server from crash"""
RESTART_COOLDOWN = 12
# util error-printing
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# ctrl+c signal handling
def signal_handler(sig, frame):
print("Exiting runner gracefully")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
# dynamic config values found
ram = DEFAULT_RAM_MB if len(sys.argv) < 2 else int(sys.argv[1])
path = None
# get path corrosponding to STARTJAR
for (_, _, files) in os.walk("."):
for filename in files:
if len([filename for x in STARTJAR if filename.startswith(x)]) != 0:
path = filename
break
break
# error if path isnt found
if path == None:
eprint(f"No filename found beginning with '{STARTJAR}' in local dir")
sys.exit(1)
# server loop
count = 0
while True:
# start server msg
if count == 0:
print("Starting server for the first time..")
else:
eprint(f"Restarting crashed server (#{count})")
# run server
os.system(f"java -Xmx{ram}m -jar {path}")
# on crash
eprint(f"Server crashed, restarting in {RESTART_COOLDOWN} second(s)")
count += 1
time.sleep(RESTART_COOLDOWN)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment