From 45d4e74c98a613991127fc6fdb851c8c51f26d56 Mon Sep 17 00:00:00 2001 From: H3CoF6 <1707889225@qq.com> Date: Sat, 21 Mar 2026 02:14:38 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dlinux=E6=89=93?= =?UTF-8?q?=E5=8C=85=E5=90=8E=E6=97=A0=E6=B3=95=E6=AD=A3=E5=B8=B8=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E8=BF=9B=E7=A8=8B=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- electron/services/keyServiceLinux.ts | 67 +++++++++++++++++++++++----- package.json | 2 +- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/electron/services/keyServiceLinux.ts b/electron/services/keyServiceLinux.ts index 3af9ddf..8fe2cc4 100644 --- a/electron/services/keyServiceLinux.ts +++ b/electron/services/keyServiceLinux.ts @@ -46,8 +46,32 @@ export class KeyServiceLinux { onStatus?: (message: string, level: number) => void ): Promise { try { + // 1. 构造一个包含常用系统命令路径的环境变量,防止打包后找不到命令 + const envWithPath = { + ...process.env, + PATH: `${process.env.PATH || ''}:/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin` + }; + onStatus?.('正在尝试结束当前微信进程...', 0) - await execAsync('killall -9 wechat wechat-bin xwechat').catch(() => {}) + console.log('[Debug] 开始执行进程清理逻辑...'); + + try { + const { stdout, stderr } = await execAsync('killall -9 wechat wechat-bin xwechat', { env: envWithPath }); + console.log(`[Debug] killall 成功退出. stdout: ${stdout}, stderr: ${stderr}`); + } catch (err: any) { + // 命令如果没找到进程通常会返回 code 1,这也是正常的,但我们需要记录下来 + console.log(`[Debug] killall 报错或未找到进程: ${err.message}`); + + // Fallback: 尝试使用 pkill 兜底 + try { + console.log('[Debug] 尝试使用备用命令 pkill...'); + await execAsync('pkill -9 -x "wechat|wechat-bin|xwechat"', { env: envWithPath }); + console.log('[Debug] pkill 执行完成'); + } catch (e: any) { + console.log(`[Debug] pkill 报错或未找到进程: ${e.message}`); + } + } + // 稍微等待进程完全退出 await new Promise(r => setTimeout(r, 1000)) @@ -76,11 +100,14 @@ export class KeyServiceLinux { env: cleanEnv }); - child.on('error', () => {}); + child.on('error', (err) => { + console.log(`[Debug] 拉起 ${binName} 失败:`, err.message); + }); child.unref(); - } catch (e) { - + console.log(`[Debug] 尝试拉起 ${binName} 完毕`); + } catch (e: any) { + console.log(`[Debug] 尝试拉起 ${binName} 发生异常:`, e.message); } } @@ -88,16 +115,35 @@ export class KeyServiceLinux { let pid = 0 for (let i = 0; i < 15; i++) { // 最多等 15 秒 await new Promise(r => setTimeout(r, 1000)) - const { stdout } = await execAsync('pidof wechat wechat-bin xwechat').catch(() => ({ stdout: '' })) - const pids = stdout.trim().split(/\s+/).filter(p => p) - if (pids.length > 0) { - pid = parseInt(pids[0], 10) - break + + try { + const { stdout } = await execAsync('pidof wechat wechat-bin xwechat', { env: envWithPath }); + const pids = stdout.trim().split(/\s+/).filter(p => p); + if (pids.length > 0) { + pid = parseInt(pids[0], 10); + console.log(`[Debug] 第 ${i + 1} 秒,通过 pidof 成功获取 PID: ${pid}`); + break; + } + } catch (err: any) { + console.log(`[Debug] 第 ${i + 1} 秒,pidof 失败: ${err.message.split('\n')[0]}`); + + // Fallback: 使用 pgrep 兜底 + try { + const { stdout: pgrepOut } = await execAsync('pgrep -x "wechat|wechat-bin|xwechat"', { env: envWithPath }); + const pids = pgrepOut.trim().split(/\s+/).filter(p => p); + if (pids.length > 0) { + pid = parseInt(pids[0], 10); + console.log(`[Debug] 第 ${i + 1} 秒,通过 pgrep 成功获取 PID: ${pid}`); + break; + } + } catch (e: any) { + console.log(`[Debug] 第 ${i + 1} 秒,pgrep 也失败: ${e.message.split('\n')[0]}`); + } } } if (!pid) { - const err = '未能自动启动微信,请手动启动并登录。' + const err = '未能自动启动微信,或获取PID失败,请查看控制台日志或手动启动并登录。' onStatus?.(err, 2) return { success: false, error: err } } @@ -108,6 +154,7 @@ export class KeyServiceLinux { return await this.getDbKey(pid, onStatus) } catch (err: any) { + console.error('[Debug] 自动获取流程彻底崩溃:', err); const errMsg = '自动获取微信 PID 失败: ' + err.message onStatus?.(errMsg, 2) return { success: false, error: errMsg } diff --git a/package.json b/package.json index b6ec942..213700d 100644 --- a/package.json +++ b/package.json @@ -95,7 +95,7 @@ "linux": { "icon": "public/icon.png", "target": [ - "deb", + "appimage", "tar.gz" ], "category": "Utility",