Start und stopp skript für Java Server

main
Alex 6 months ago
parent 069cc6a8d7
commit e03fca6f12

@ -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()

@ -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()
Loading…
Cancel
Save