WARNING: USE AT YOUR OWN RISK.
Create a folder on any disk, for example, D:\python_scripts
and add that folder to the system environment variable path. create new file eg: 'xampp.py' in that folder.
paste this code and rename the xampp_path
variable to the path where you have installed xampp.
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(service_name):
command = ["killprocess.bat", service_name]
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, creationflags=subprocess.CREATE_NO_WINDOW)
output, error = process.communicate()
if process.returncode != 0:
print(f"Failed to stop {service_name}. Error: {error.decode()}")
else:
print(f"Successfully stopped {service_name}. Output: {output.decode()}")
# 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)
Now you can run the script from the command line using the following commands:
to start php and mysql
xampp.py --start
to stop php and mysql
xampp.py --stop