From 26562b1ca41a97041fe7681f24cca22f10f09de3 Mon Sep 17 00:00:00 2001 From: oliver Date: Wed, 29 Apr 2026 01:19:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BE=AE=E4=BF=A1=E4=B8=8E?= =?UTF-8?q?=20WhatsApp=20=E5=81=9C=E6=AD=A2=E8=84=9A=E6=9C=AC=E5=9C=A8?= =?UTF-8?q?=E8=BF=9B=E7=A8=8B=E5=B7=B2=E9=80=80=E5=87=BA=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E8=AF=AF=E6=8A=A5=E5=BC=82=E5=B8=B8=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 统一使用静默 taskkill 包装函数处理 sidecar 进程清理,避免 NativeCommandError 噪声并修正 PowerShell 只读变量名冲突,保证 start_all/status_all/stop_all 链路稳定结束。 Made-with: Cursor --- runtime/operations/scripts/weixin_start.ps1 | 19 ++++--- runtime/operations/scripts/weixin_stop.ps1 | 56 ++++++++++---------- runtime/operations/scripts/whatsapp_stop.ps1 | 20 +++++-- 3 files changed, 55 insertions(+), 40 deletions(-) diff --git a/runtime/operations/scripts/weixin_start.ps1 b/runtime/operations/scripts/weixin_start.ps1 index b2a02780..9235a0f1 100644 --- a/runtime/operations/scripts/weixin_start.ps1 +++ b/runtime/operations/scripts/weixin_start.ps1 @@ -36,16 +36,19 @@ function Get-SidecarProcesses { } } +function Invoke-TaskKillQuiet { + param( + [Parameter(Mandatory = $true)] + [string]$ProcessId + ) + cmd.exe /c "taskkill /PID $ProcessId /T /F 1>nul 2>nul" | Out-Null + return $LASTEXITCODE +} + function Stop-SidecarProcesses { $procs = @(Get-SidecarProcesses | Sort-Object ProcessId -Descending) - $oldNativePref = $PSNativeCommandUseErrorActionPreference - try { - $PSNativeCommandUseErrorActionPreference = $false - foreach ($proc in $procs) { - taskkill.exe /PID $proc.ProcessId /T /F 2>$null | Out-Null - } - } finally { - $PSNativeCommandUseErrorActionPreference = $oldNativePref + foreach ($proc in $procs) { + [void](Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId)) } return $procs.Count } diff --git a/runtime/operations/scripts/weixin_stop.ps1 b/runtime/operations/scripts/weixin_stop.ps1 index 834865d5..eb96f811 100644 --- a/runtime/operations/scripts/weixin_stop.ps1 +++ b/runtime/operations/scripts/weixin_stop.ps1 @@ -37,27 +37,33 @@ function Get-SidecarProcesses { } } +function Invoke-TaskKillQuiet { + param( + [Parameter(Mandatory = $true)] + [string]$ProcessId, + [switch]$ForceKill + ) + $forceArg = "" + if ($ForceKill) { $forceArg = " /F" } + cmd.exe /c "taskkill /PID $ProcessId /T$forceArg 1>nul 2>nul" | Out-Null + return $LASTEXITCODE +} + function Stop-SidecarProcesses { param( [switch]$ForceKill ) $procs = @(Get-SidecarProcesses | Sort-Object ProcessId -Descending) - $oldNativePref = $PSNativeCommandUseErrorActionPreference - try { - $PSNativeCommandUseErrorActionPreference = $false - foreach ($proc in $procs) { - if ($ForceKill) { - taskkill.exe /PID $proc.ProcessId /T /F 2>$null | Out-Null - continue - } - taskkill.exe /PID $proc.ProcessId /T 2>$null | Out-Null - if ($LASTEXITCODE -ne 0) { - # Some process trees require force kill on Windows. - taskkill.exe /PID $proc.ProcessId /T /F 2>$null | Out-Null - } + foreach ($proc in $procs) { + if ($ForceKill) { + [void](Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId) -ForceKill) + continue + } + $code = Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId) + if ($code -ne 0) { + # Some process trees require force kill on Windows. + [void](Invoke-TaskKillQuiet -ProcessId ([string]$proc.ProcessId) -ForceKill) } - } finally { - $PSNativeCommandUseErrorActionPreference = $oldNativePref } return $procs.Count } @@ -79,20 +85,14 @@ if (-not $procId) { exit 0 } -$oldNativePref = $PSNativeCommandUseErrorActionPreference -try { - $PSNativeCommandUseErrorActionPreference = $false - if ($Force) { - taskkill.exe /PID $procId /T /F 2>$null | Out-Null - } else { - taskkill.exe /PID $procId /T 2>$null | Out-Null - if ($LASTEXITCODE -ne 0) { - # Retry with /F to avoid noisy parent/child kill failures. - taskkill.exe /PID $procId /T /F 2>$null | Out-Null - } +if ($Force) { + [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill) +} else { + $code = Invoke-TaskKillQuiet -ProcessId ([string]$procId) + if ($code -ne 0) { + # Retry with /F to avoid noisy parent/child kill failures. + [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill) } -} finally { - $PSNativeCommandUseErrorActionPreference = $oldNativePref } $killed = Stop-SidecarProcesses -ForceKill:$Force diff --git a/runtime/operations/scripts/whatsapp_stop.ps1 b/runtime/operations/scripts/whatsapp_stop.ps1 index 9e4eda4b..79822ee2 100644 --- a/runtime/operations/scripts/whatsapp_stop.ps1 +++ b/runtime/operations/scripts/whatsapp_stop.ps1 @@ -19,15 +19,27 @@ $oclawRoot = Resolve-RepoRoot $sidecarRoot = Join-Path $oclawRoot "data\\channel_sidecar\\$ChannelId" $sidecarPidFile = Join-Path $sidecarRoot "pid.txt" +function Invoke-TaskKillQuiet { + param( + [Parameter(Mandatory = $true)] + [string]$ProcessId, + [switch]$ForceKill + ) + $forceArg = "" + if ($ForceKill) { $forceArg = " /F" } + cmd.exe /c "taskkill /PID $ProcessId /T$forceArg 1>nul 2>nul" | Out-Null + return $LASTEXITCODE +} + if (Test-Path $sidecarPidFile) { $procId = (Get-Content -Path $sidecarPidFile -ErrorAction SilentlyContinue | Select-Object -First 1) if ($procId) { if ($Force) { - taskkill.exe /PID $procId /T /F 2>$null | Out-Null + [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill) } else { - taskkill.exe /PID $procId /T 2>$null | Out-Null - if ($LASTEXITCODE -ne 0) { - taskkill.exe /PID $procId /T /F 2>$null | Out-Null + $code = Invoke-TaskKillQuiet -ProcessId ([string]$procId) + if ($code -ne 0) { + [void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill) } } }