stop server django programmatically

import os
import signal
import psutil

def stop_django_server():
    # Step 1: Get the process ID (PID) of the Django development server
    server_pid = None
    for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
        if 'python' in proc.info['name'] and 'manage.py' in proc.info['cmdline']:
            server_pid = proc.info['pid']
            break

    # Step 2: Check if the Django server process ID was found
    if server_pid is not None:
        # Step 3: Send a SIGINT signal to gracefully stop the Django server
        os.kill(server_pid, signal.SIGINT)
        print(f"Django server with PID {server_pid} stopped.")
    else:
        print("Django server process not found.")

# Call the function to stop the Django server
stop_django_server()