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.

36 lines
1.1 KiB
Python

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