From 97ecf8e605eb7809740d308770c7390c60ce3145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=96=87=E8=96=87=E5=AE=89?= Date: Tue, 3 Feb 2026 11:35:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=94=A8=E6=88=B7=E4=BA=A4?= =?UTF-8?q?=E6=98=93=E7=8A=B6=E6=80=81=E4=B8=8D=E6=AD=A3=E5=B8=B8=E6=98=BE?= =?UTF-8?q?=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/api/supervisor_account.py | 43 +++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/backend/api/supervisor_account.py b/backend/api/supervisor_account.py index cb0d288..a6db804 100644 --- a/backend/api/supervisor_account.py +++ b/backend/api/supervisor_account.py @@ -19,7 +19,7 @@ import subprocess import sys from dataclasses import dataclass from pathlib import Path -from typing import Optional, Tuple +from typing import Optional, Tuple, List, Dict, Any DEFAULT_CANDIDATE_CONFS = [ @@ -29,6 +29,38 @@ DEFAULT_CANDIDATE_CONFS = [ "/etc/supervisord.conf", ] +# 常见 supervisorctl 路径候选 +DEFAULT_SUPERVISORCTL_CANDIDATES = [ + "/www/server/panel/pyenv/bin/supervisorctl", + "/usr/bin/supervisorctl", + "/usr/local/bin/supervisorctl", + "/usr/local/python/bin/supervisorctl", +] + +def _detect_supervisorctl_path() -> str: + """ + 探测 supervisorctl 可执行文件路径 + """ + env_path = (os.getenv("SUPERVISORCTL_PATH") or "").strip() + if env_path: + return env_path + + # 优先检查 PATH 中的 supervisorctl + import shutil + if shutil.which("supervisorctl"): + return "supervisorctl" + + # 检查常见绝对路径 + for p in DEFAULT_SUPERVISORCTL_CANDIDATES: + try: + if os.path.exists(p) and os.access(p, os.X_OK): + return p + except Exception: + continue + + return "supervisorctl" # 兜底 + + # 常见 supervisord 主日志路径候选(不同发行版/面板插件差异很大) DEFAULT_SUPERVISORD_LOG_CANDIDATES = [ # aaPanel / 宝塔 supervisor 插件常见 @@ -226,7 +258,7 @@ def write_program_ini(program_dir: Path, filename: str, content: str) -> Path: def _build_supervisorctl_cmd(args: list[str]) -> list[str]: - supervisorctl_path = os.getenv("SUPERVISORCTL_PATH", "supervisorctl") + supervisorctl_path = _detect_supervisorctl_path() supervisor_conf = (os.getenv("SUPERVISOR_CONF") or "").strip() use_sudo = (os.getenv("SUPERVISOR_USE_SUDO", "false") or "false").lower() == "true" @@ -250,8 +282,15 @@ def run_supervisorctl(args: list[str], timeout_sec: int = 10) -> str: res = subprocess.run(cmd, capture_output=True, text=True, timeout=int(timeout_sec)) except subprocess.TimeoutExpired: raise RuntimeError("supervisorctl 超时") + except FileNotFoundError: + # 明确提示找不到命令,帮助排查路径问题 + cmd_str = " ".join(cmd) + raise RuntimeError(f"Command not found: {cmd[0]} (Full cmd: {cmd_str})") + except Exception as e: + raise RuntimeError(f"supervisorctl execution failed: {str(e)}") out = (res.stdout or "").strip() + err = (res.stderr or "").strip() combined = "\n".join([s for s in [out, err] if s]).strip() # supervisorctl: status 在存在 STOPPED 等进程时可能返回 exit=3,但输出仍然有效