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.
46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
6 months ago
|
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()
|