I have this code for start and stopping xampp apache and mysql.
import subprocess
import os
import time
import argparse
import sys
# XAMPP installation path
xampp_path = 'D:\projects' # Get path from environment variable or use default
# Seconds to wait for services to start/stop
wait_time = 2
# Function to start a service using batch files
def start_service(batch_file):
command = os.path.join(xampp_path, batch_file)
if os.path.isfile(command):
print(f"Starting service with {batch_file}...")
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
time.sleep(wait_time) # Give it some time to start
print(f"Service started successfully with {batch_file}.")
else:
print(f"Batch file {batch_file} does not exist.")
# Function to stop a service using batch files
def stop_service(batch_file):
command = os.path.join(xampp_path, batch_file)
if os.path.isfile(command):
print(f"Stopping service with {batch_file}...")
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
time.sleep(wait_time) # Give it some time to stop
print(f"Attempted to stop service with {batch_file}.")
else:
print(f"Batch file {batch_file} does not exist.")
# Function to forcefully kill a service by its process name
def kill_service(service_name):
try:
subprocess.run(f"taskkill /F /IM {service_name}", check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, creationflags=subprocess.CREATE_NO_WINDOW)
print(f"Service {service_name} killed successfully.")
except subprocess.CalledProcessError as e:
print(f"Error killing {service_name}: {e.stderr.decode()}")
# Parse command line arguments
parser = argparse.ArgumentParser(description="Start or stop XAMPP services.")
parser.add_argument("--start", action="store_true", help="Start XAMPP services")
parser.add_argument("--stop", action="store_true", help="Stop XAMPP services")
args = parser.parse_args()
# Start or stop XAMPP services based on command line arguments
if args.start:
# Start Apache and MySQL
start_service("apache_start.bat")
time.sleep(wait_time) # Wait a few seconds before starting MySQL to ensure Apache starts properly
start_service("mysql_start.bat")
elif args.stop:
# Stop Apache and MySQL
stop_service("apache_stop.bat")
stop_service("mysql_stop.bat")
# Kill lingering processes after attempting to stop them gracefully
kill_service("xampp-control.exe") # xampp
kill_service("httpd.exe") # Apache
kill_service("mysqld.exe") # MySQL
# Check if services are still running
def check_service_stopped(service_name):
tasklist_output = subprocess.check_output("tasklist", shell=True).decode()
if service_name in tasklist_output:
print(f"{service_name} is still running.")
else:
print(f"{service_name} has been stopped.")
# Check Apache and MySQL status
check_service_stopped("httpd.exe") # Apache
check_service_stopped("mysqld.exe") # MySQL
print("Apache and MySQL services have been stopped.")
else:
print("Please specify --start or --stop parameter.")
sys.exit(0)
I have the file in a folder that is set in environment variables, so i’m able to run it from anywhere using:
xampp.py --start
or to stop
xampp.py --stop
Problem:
both works. but when i run xampp.py –stop it opens py.exe does its job and closes it. but along with that it opens 2 new cmd windows. and i cannot figure out why and from where it opens those.
both cmd window shows this:
The system cannot find the path specified.
D:projects>