修复桌面应用无限多开:GUI 启动不再走 launchctl submit

launchctl submit 的任务带自动重生语义,open -n 立即退出会被 launchd
反复重启,每次重启 open -n 新开一个应用实例(约 20 秒一个)。
改为直接同步 open(不带 -n),已有实例时聚焦不新开;
实测启动后 45 秒稳定单实例。
This commit is contained in:
logan committed 2026-09-20 00:48:38 +08:00
1 parent 39f4d5addc
commit 02efc13a21
1 file changed
+19 -25
+19 -25
View File
@@ -3,7 +3,7 @@
#
# ./run.sh # = web
# ./run.sh web # Gin 代理 + Vite(浏览器)
# ./run.sh gui # Vite + Tauri 桌面(Gin 可选;默认也拉起方便对照)
# ./run.sh gui # Vite + 临时 .app 桌面(Dock 用当前 icon.icns)
# ./run.sh all # Gin + Vite + Desktop
# ./run.sh server|web-only|desktop # 单组件
# ./run.sh stop|restart|status|help
@@ -38,6 +38,9 @@ SERVER_LOG="$LOG_DIR/server.log"
WEB_LOG="$LOG_DIR/web.log"
DESKTOP_LOG="$LOG_DIR/desktop.log"
# shellcheck source=scripts/gui-dev-app.sh
source "$SCRIPT_DIR/scripts/gui-dev-app.sh"
export PATH="/Users/logan/.nvm/versions/node/v24.19.0/bin:/opt/homebrew/bin:/usr/local/bin:${HOME}/.cargo/bin:$PATH"
check_port() { lsof -ti:"$1" 2>/dev/null | head -1 || true; }
@@ -151,32 +154,27 @@ stop_web() {
}
start_desktop() {
# GUI 依赖同一份 Vite;先保证 5173
# Vite 照常(浏览器联调);Dock 桌面走临时 .app(每次刷当前 icons/icon.icns)
start_web
info "启动桌面 OpsxAPI ..."
info "启动桌面 OpsxAPI(临时 .app + 当前图标)..."
: >"$DESKTOP_LOG"
ensure_npm
# 清掉旧的 debug 进程(二进制名 OpsxAPI)
pkill -f 'target/debug/OpsxAPI' 2>/dev/null || true
pkill -f 'target/debug/opsxapi-desktop' 2>/dev/null || true
stop_detached "$DESKTOP_LABEL"
start_detached "$DESKTOP_LABEL" "$DESKTOP_LOG" \
"export PATH=\"/Users/logan/.nvm/versions/node/v24.19.0/bin:/opt/homebrew/bin:/usr/local/bin:\$HOME/.cargo/bin:\$PATH\"; cd \"$SCRIPT_DIR/apps/desktop\" && exec npm run tauri -- dev"
kill_opsxapi_gui
ensure_gui_dev_app || return 1
# 注意:不能用 launchctl submit 跑 open —— submit 任务有自动重生语义,
# open 立即退出会被 launchd 反复重启,导致桌面应用每 ~20s 多开一个实例。
# 这里直接同步 open(不带 -n:万一已有实例则聚焦,不再新开)。
open "$GUI_APP" 2>>"$DESKTOP_LOG" || { error "open 失败,见 $DESKTOP_LOG"; return 1; }
local i=0
while true; do
if pgrep -f 'target/debug/OpsxAPI' >/dev/null 2>&1 \
|| pgrep -f 'target/debug/opsxapi-desktop' >/dev/null 2>&1; then
success "桌面 OpsxAPI 已启动(Dock 名应为 OpsxAPI)"
if pgrep -x OpsxAPI >/dev/null 2>&1; then
success "桌面 OpsxAPI 已启动(临时 .app,Dock 用当前 icon.icns)"
info "路径: $GUI_APP"
return 0
fi
if grep -Eiq 'error:|panicked|could not compile' "$DESKTOP_LOG" 2>/dev/null; then
error "桌面启动失败,见 $DESKTOP_LOG"
tail -n 40 "$DESKTOP_LOG" || true
return 1
fi
i=$((i + 1))
if [ "$i" -ge 120 ]; then
if [ "$i" -ge 30 ]; then
error "桌面启动超时,见 $DESKTOP_LOG"
tail -n 40 "$DESKTOP_LOG" || true
return 1
@@ -186,10 +184,7 @@ start_desktop() {
}
stop_desktop() {
stop_detached "$DESKTOP_LABEL"
pkill -f 'target/debug/OpsxAPI' 2>/dev/null || true
pkill -f 'target/debug/opsxapi-desktop' 2>/dev/null || true
pkill -f 'apps/desktop/node_modules/.bin/tauri' 2>/dev/null || true
kill_opsxapi_gui
success "桌面已停止"
}
@@ -205,8 +200,7 @@ status() {
else
warning "web :$WEB_PORT DOWN"
fi
if pgrep -f 'target/debug/OpsxAPI' >/dev/null 2>&1 \
|| pgrep -f 'target/debug/opsxapi-desktop' >/dev/null 2>&1; then
if pgrep -x OpsxAPI >/dev/null 2>&1; then
success "gui OpsxAPI UP"
else
warning "gui OpsxAPI DOWN"
@@ -219,7 +213,7 @@ $PROJECT_NAME 启动脚本(场景 profile)
./run.sh # 同 web
./run.sh web # 浏览器:Gin + Vite
./run.sh gui # 桌面:Vite + Tauri(默认也起 Gin,OPSXAPI_GUI_WITH_SERVER=0 可关)
./run.sh gui # 桌面:Vite + 临时 .app(Dock=icons/icon.icns)
./run.sh all # Gin + Vite + Desktop
./run.sh server # 只 Gin