oclaw/runtime/operations/scripts/whatsapp_stop.ps1
oliver 26562b1ca4 修复微信与 WhatsApp 停止脚本在进程已退出时的误报异常。
统一使用静默 taskkill 包装函数处理 sidecar 进程清理,避免 NativeCommandError 噪声并修正 PowerShell 只读变量名冲突,保证 start_all/status_all/stop_all 链路稳定结束。

Made-with: Cursor
2026-04-29 01:19:00 +08:00

49 lines
1.3 KiB
PowerShell

param(
[string]$ChannelId = "whatsapp",
[switch]$Force
)
$ErrorActionPreference = "Stop"
function Resolve-RepoRoot {
$here = Split-Path -Parent $PSCommandPath
return (Resolve-Path (Join-Path $here "..\\..\\..")).Path
}
$systemNodeDir = "C:\\Program Files\\nodejs"
if (Test-Path (Join-Path $systemNodeDir "node.exe")) {
$env:PATH = "$systemNodeDir;$env:PATH"
}
$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) {
[void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill)
} else {
$code = Invoke-TaskKillQuiet -ProcessId ([string]$procId)
if ($code -ne 0) {
[void](Invoke-TaskKillQuiet -ProcessId ([string]$procId) -ForceKill)
}
}
}
Remove-Item -Force $sidecarPidFile -ErrorAction SilentlyContinue
}
Write-Host "[ok] stopped whatsapp channel=$ChannelId"