diff --git a/backend/api/routes/system.py b/backend/api/routes/system.py index 6535c4f..6baa722 100644 --- a/backend/api/routes/system.py +++ b/backend/api/routes/system.py @@ -1355,18 +1355,35 @@ async def backend_stop(_admin: Dict[str, Any] = Depends(require_system_admin)) - def _recommendations_process_running() -> Tuple[bool, Optional[int]]: - """检查推荐服务进程是否运行,返回 (running, pid)""" + """检查推荐服务进程是否运行,返回 (running, pid)。兼容 pgrep/ps 及 supervisor 等启动方式。""" + # 1. 优先 pgrep(Linux/macOS 常见) + for pattern in ["trading_system.recommendations_main", "recommendations_main"]: + try: + result = subprocess.run( + ["pgrep", "-f", pattern], + capture_output=True, + text=True, + timeout=5, + ) + if result.returncode == 0 and result.stdout.strip(): + pids = [x for x in result.stdout.strip().split() if x.isdigit()] + if pids: + return True, int(pids[0]) + except (FileNotFoundError, subprocess.TimeoutExpired, ValueError): + break + + # 2. 回退:ps + grep(pgrep 不可用或匹配失败时) try: result = subprocess.run( - ["pgrep", "-f", "trading_system.recommendations_main"], + ["sh", "-c", "ps aux 2>/dev/null | grep -E 'recommendations_main|trading_system.recommendations' | grep -v grep | head -1"], capture_output=True, text=True, timeout=5, ) if result.returncode == 0 and result.stdout.strip(): - pids = result.stdout.strip().split() - pid = int(pids[0]) if pids else None - return True, pid + parts = result.stdout.strip().split() + if len(parts) >= 2 and parts[1].isdigit(): + return True, int(parts[1]) except (FileNotFoundError, subprocess.TimeoutExpired, ValueError): pass return False, None