Files
gogo2/scripts/kill_stale_processes.py
Dobromir Popov 1f47576723 training fixes
2025-06-26 14:18:04 +03:00

156 lines
5.5 KiB
Python

#!/usr/bin/env python3
"""
Kill Stale Processes Script
Safely terminates stale Python processes related to the trading dashboard
with proper error handling and graceful termination.
"""
import os
import sys
import time
import signal
from pathlib import Path
def kill_stale_processes():
"""Kill stale trading dashboard processes safely"""
try:
import psutil
except ImportError:
print("psutil not available - using fallback method")
return kill_stale_fallback()
current_pid = os.getpid()
killed_processes = []
failed_processes = []
# Keywords to identify trading dashboard processes
target_keywords = [
'dashboard', 'scalping', 'trading', 'tensorboard',
'run_clean', 'run_main', 'gogo2', 'mexc'
]
try:
print("Scanning for stale processes...")
# Get all Python processes
python_processes = []
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
try:
if proc.info['pid'] == current_pid:
continue
name = proc.info['name'].lower()
if 'python' in name or 'tensorboard' in name:
cmdline_str = ' '.join(proc.info['cmdline']) if proc.info['cmdline'] else ''
# Check if this is a target process
if any(keyword in cmdline_str.lower() for keyword in target_keywords):
python_processes.append({
'proc': proc,
'pid': proc.info['pid'],
'name': proc.info['name'],
'cmdline': cmdline_str
})
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
continue
if not python_processes:
print("No stale processes found")
return True
print(f"Found {len(python_processes)} target processes to terminate:")
for p in python_processes:
print(f" - PID {p['pid']}: {p['name']} - {p['cmdline'][:80]}...")
# Graceful termination first
print("\nAttempting graceful termination...")
for p in python_processes:
try:
proc = p['proc']
if proc.is_running():
proc.terminate()
print(f" Sent SIGTERM to PID {p['pid']}")
except Exception as e:
failed_processes.append(f"Failed to terminate PID {p['pid']}: {e}")
# Wait for graceful shutdown
time.sleep(2.0)
# Force kill remaining processes
print("\nChecking for remaining processes...")
for p in python_processes:
try:
proc = p['proc']
if proc.is_running():
print(f" Force killing PID {p['pid']} ({p['name']})")
proc.kill()
killed_processes.append(f"Force killed PID {p['pid']} ({p['name']})")
else:
killed_processes.append(f"Gracefully terminated PID {p['pid']} ({p['name']})")
except (psutil.NoSuchProcess, psutil.AccessDenied):
killed_processes.append(f"Process PID {p['pid']} already terminated")
except Exception as e:
failed_processes.append(f"Failed to kill PID {p['pid']}: {e}")
# Results
print(f"\n=== Process Cleanup Results ===")
if killed_processes:
print(f"Successfully cleaned up {len(killed_processes)} processes:")
for msg in killed_processes:
print(f"{msg}")
if failed_processes:
print(f"\nFailed to clean up {len(failed_processes)} processes:")
for msg in failed_processes:
print(f"{msg}")
print(f"\nCleanup completed. {len(killed_processes)} processes terminated.")
return len(failed_processes) == 0
except Exception as e:
print(f"Error during process cleanup: {e}")
return False
def kill_stale_fallback():
"""Fallback method using basic OS commands"""
print("Using fallback process killing method...")
try:
if os.name == 'nt': # Windows
import subprocess
# Kill Python processes with dashboard keywords
result = subprocess.run([
'taskkill', '/f', '/im', 'python.exe'
], capture_output=True, text=True)
if result.returncode == 0:
print("Windows: Killed all Python processes")
else:
print("Windows: No Python processes to kill or access denied")
else: # Unix/Linux
import subprocess
# More targeted approach for Unix
subprocess.run(['pkill', '-f', 'dashboard'], capture_output=True)
subprocess.run(['pkill', '-f', 'scalping'], capture_output=True)
subprocess.run(['pkill', '-f', 'tensorboard'], capture_output=True)
print("Unix: Killed dashboard-related processes")
return True
except Exception as e:
print(f"Fallback method failed: {e}")
return False
if __name__ == "__main__":
print("=" * 50)
print("STALE PROCESS CLEANUP")
print("=" * 50)
success = kill_stale_processes()
exit_code = 0 if success else 1
print("=" * 50)
sys.exit(exit_code)