From e03fca6f12609010cc739a12442568eafe939c12 Mon Sep 17 00:00:00 2001 From: Alex Date: Sun, 14 Jul 2024 23:11:48 +0200 Subject: [PATCH] =?UTF-8?q?Start=20und=20stopp=20skript=20f=C3=BCr=20Java?= =?UTF-8?q?=20Server?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- start_server.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ stop__server.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 start_server.py create mode 100644 stop__server.py diff --git a/start_server.py b/start_server.py new file mode 100644 index 0000000..40de8c6 --- /dev/null +++ b/start_server.py @@ -0,0 +1,46 @@ +import subprocess +import os +import argparse + +def main(): + parser = argparse.ArgumentParser(description="Start the Java server") + parser.add_argument("name", help="The base name for the server JAR and PID file") + parser.add_argument("--java_path", help="The path to the specific Java version's bin directory", required=True) + args = parser.parse_args() + + # Get the current script directory and its name + script_dir = os.path.dirname(os.path.abspath(__file__)) + dir_name = os.path.basename(script_dir) + + # Path to your Java application JAR file + JAVA_APP_PATH = os.path.join(script_dir, args.name + '.jar') + + # Path to the PID file + PID_FILE = os.path.join(script_dir, args.name + '.pid') + + # Unique identifier for the Java application (using the directory name) + UNIQUE_IDENTIFIER = dir_name + + # Check if the PID file already exists + if os.path.exists(PID_FILE): + with open(PID_FILE, 'r') as file: + pid = file.read().strip() + print(f"Server is already running. PID={pid}") + exit(1) + + # Path to the specific java.exe + JAVA_EXECUTABLE = os.path.join(args.java_path, 'java.exe') + + # Start the Java application in the background with a unique identifier + process = subprocess.Popen([JAVA_EXECUTABLE, '-Dapp.id=' + UNIQUE_IDENTIFIER, '-Xmx1024M', '-Xms1024M', '-jar', JAVA_APP_PATH, '--nogui', '--bonusChest'], + stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) + + # Save the PID of the Java application + pid = process.pid + with open(PID_FILE, 'w') as file: + file.write(str(pid)) + + print(f"Server started with PID={pid}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/stop__server.py b/stop__server.py new file mode 100644 index 0000000..5e16d8a --- /dev/null +++ b/stop__server.py @@ -0,0 +1,36 @@ +import os +import subprocess +import argparse + +def main(): + parser = argparse.ArgumentParser(description="Stop the Java server") + parser.add_argument("name", help="The base name for the server PID file") + args = parser.parse_args() + + # Get the current script directory + script_dir = os.path.dirname(os.path.abspath(__file__)) + + # Path to the PID file + PID_FILE = os.path.join(script_dir, args.name + '.pid') + + # Check if the PID file exists + if not os.path.exists(PID_FILE): + print("PID file not found. Is the server running?") + exit(1) + + # Read the PID from the file + with open(PID_FILE, 'r') as file: + pid = int(file.read().strip()) + + # Check if the process is running and terminate it + try: + # Use taskkill command to terminate the process on Windows + subprocess.run(['taskkill', '/F', '/PID', str(pid)], check=True) + print("Server stopped.") + os.remove(PID_FILE) + except subprocess.CalledProcessError: + print(f"No process found with PID={pid}.") + os.remove(PID_FILE) + +if __name__ == "__main__": + main() \ No newline at end of file