ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Windows 本地化部署 OpenClaw:用 NSSM 实现开机无窗口自启、进程守护与宕机自动重启并接入微信

Windows 本地化部署 OpenClaw:用 NSSM 实现开机无窗口自启、进程守护与宕机自动重启并接入微信 1. 为什么手动开终端跑 OpenClaw 迟早会出问题OpenClaw 在 Windows 上跑起来其实不难npm install -g openclawlatest之后一条openclaw gateway --port 18789就能把 Gateway 拉起来浏览器打开http://127.0.0.1:18789/就能看到 Dashboard。问题从来不在“能不能跑”而在“能不能一直跑”。我见过太多人第一周用得好好的第二周开始发现微信消息不回了第三周干脆忘了这东西还在不在——因为那个黑乎乎的终端窗口被自己顺手关掉了。手动启动的痛点非常具体每次开机要手动敲命令终端一关服务就断进程因为某个插件异常崩了没人拉起来窗口常驻任务栏看着烦更麻烦的是如果你同时用 OpenClaw 自带的计划任务和手动启动两个 Gateway 会抢同一个 18789 端口日志里全是EADDRINUSE排查起来一头雾水。这篇要解决的就是“长期稳定运行”这件事。目标很明确把 OpenClaw Gateway 用 NSSM 注册成 Windows 系统服务做到开机无窗口自启、进程异常退出自动拉起、服务级宕机自动重启最后再把微信渠道接进来。整套方案的核心链路是Windows 开机 → Windows Service Control Manager → NSSM 托管的 OpenClawGateway 服务 → OpenClaw Gateway (node.exe) → 127.0.0.1:18789 → Dashboard / RPC / 插件 / 微信渠道适合谁看在 Windows 10/11 单机上部署 OpenClaw、希望它像系统服务一样安静后台运行、并且要接微信做消息渠道的人。如果你只是临时跑一下试试那手动命令就够了但只要你想让它“开机就在、崩了自己起来”NSSM 这条路是 Windows 上最省心的选择。下面从环境准备一路写到微信连通性验证命令都可以直接复制。2. 用 NSSM 把 OpenClaw 注册成 Windows 服务的前置准备在动手注册服务之前有几件事必须先确认否则后面报错会很难定位。这一节把环境、权限、旧任务清理三件事讲清楚。先说环境。系统建议 Windows 10 或 Windows 11Node.js 要装好并且node.exe在 PATH 里OpenClaw 通过 npm 全局安装。打开一个普通 PowerShell 先验证node -v npm -v npm install -g openclawlatest openclaw --version where.exe openclawwhere.exe openclaw应该能输出一个.cmd路径通常在 npm 全局 bin 目录下。如果这一步找不到后面 NSSM 一定起不来因为服务需要知道用哪个可执行文件去拉起 Gateway。接着把 Gateway 设成本地模式并看一眼基础状态openclaw config set gateway.mode local openclaw status openclaw doctor openclaw gateway status --deep默认情况下 Gateway 绑定回环地址127.0.0.1端口示例是18789。这意味着只有本机能访问不会直接暴露到局域网或公网这一点对安全很重要后面还会再提。然后是权限。创建 Windows 服务必须用管理员权限的 PowerShell。正确打开方式是开始菜单搜索 PowerShell → 右键 → 以管理员身份运行。可以用下面这条命令确认当前窗口是不是管理员net session如果返回“拒绝访问”说明不是管理员别继续往下执行。非管理员时典型报错是Administrator access is needed to install a service. OpenService(): 拒绝访问。 指定的服务未安装。遇到这个不要硬试直接关掉窗口重新以管理员身份打开。最后是清理旧任务。如果你之前用过 OpenClaw 自带的 Gateway 安装方式系统里可能已经有一个叫OpenClaw Gateway的计划任务。为了避免两个 Gateway 抢同一个端口先把它停掉删掉schtasks.exe /End /TN OpenClaw Gateway 2$null schtasks.exe /Delete /F /TN OpenClaw Gateway 2$null提示找不到任务可以忽略。本文方案统一用 NSSM 托管不再使用 OpenClaw 原生计划任务这一点后面排查Scheduled Task (missing)时会再解释。NSSM 本身用 winget 装最省事winget install NSSM.NSSM --source winget nssm version如果nssm version提示找不到命令关掉当前 PowerShell 重新开一个管理员窗口再执行Get-Command nssm.exe确认路径。装好之后前置准备就算完成了。3. 可复制的 NSSM 服务注册脚本与参数配置这一节是全文的核心给出一段可以直接粘贴到管理员 PowerShell 里执行的脚本。它的设计原则是不写死真实用户名、不写死用户目录、不写死 Node 和 OpenClaw 安装目录全部通过环境变量和命令查找自动定位这样换台机器也能用。脚本会做这些事检查管理员权限、创建状态目录和日志目录、查找openclaw.cmd和node.exe、定位 OpenClaw 的 Node 入口文件、安装或定位 NSSM、写入本地 Gateway 模式、清理旧计划任务和旧服务、创建 NSSM 服务、设置启动参数、无窗口运行、环境变量、日志重定向、进程退出重启策略、Windows 服务失败恢复最后启动并验证。# # OpenClaw Gateway NSSM Service # Windows 后台守护部署脚本 # # 1. 检查管理员权限 $currentUser [Security.Principal.WindowsIdentity]::GetCurrent() $principal New-Object Security.Principal.WindowsPrincipal($currentUser) if (-not $principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { throw 请使用“管理员身份运行”打开 PowerShell 后再执行本脚本。 } # 2. 基础配置 $ServiceName OpenClawGateway $DisplayName OpenClaw Gateway $GatewayPort 18789 # 3. 使用环境变量生成 OpenClaw 状态目录 $OpenClawUserHome $env:USERPROFILE $OpenClawStateDir Join-Path $OpenClawUserHome .openclaw $OpenClawConfig Join-Path $OpenClawStateDir openclaw.json $LogDir Join-Path $OpenClawStateDir logs\service # 4. 创建必要目录 New-Item -ItemType Directory -Force -Path $OpenClawStateDir | Out-Null New-Item -ItemType Directory -Force -Path $LogDir | Out-Null New-Item -ItemType Directory -Force -Path $env:TEMP | Out-Null # 5. 检查 OpenClaw 命令 $OpenClawCmd $null try { $OpenClawCmd (Get-Command openclaw.cmd -ErrorAction Stop).Source } catch { try { $OpenClawCmd (Get-Command openclaw -ErrorAction Stop).Source } catch { throw 找不到 openclaw 命令请确认已执行 npm install -g openclawlatest并且 npm 全局 bin 目录已加入 PATH。 } } # 6. 检查 node.exe try { $NodeExe (Get-Command node.exe -ErrorAction Stop).Source } catch { throw 找不到 node.exe请确认 Node.js 已安装并加入 PATH。 } # 7. 查找 OpenClaw Node 入口文件 $NpmRoot ( npm root -g).Trim() $OpenClawIndex Join-Path $NpmRoot openclaw\dist\index.js if (-not (Test-Path $OpenClawIndex)) { throw 找不到 OpenClaw 入口文件请确认 OpenClaw 已通过 npm 全局安装。 } # 8. 安装或定位 NSSM if (-not (Get-Command nssm.exe -ErrorAction SilentlyContinue)) { winget install NSSM.NSSM --source winget } $nssm (Get-Command nssm.exe).Source # 9. 写入 OpenClaw 本地 Gateway 模式 $OpenClawCmd config set gateway.mode local # 10. 清理 OpenClaw 原生计划任务 schtasks.exe /End /TN OpenClaw Gateway 2$null schtasks.exe /Delete /F /TN OpenClaw Gateway 2$null # 11. 删除旧 NSSM 服务 $nssm stop $ServiceName * $null $nssm remove $ServiceName confirm * $null # 12. 组合 PATH $MachinePath [Environment]::GetEnvironmentVariable(Path, Machine) $UserPath [Environment]::GetEnvironmentVariable(Path, User) $NpmBin Split-Path $OpenClawCmd -Parent $NodeBin Split-Path $NodeExe -Parent $CombinedPath $NodeBin;$NpmBin;$MachinePath;$UserPath # 13. 创建 NSSM 服务 $nssm install $ServiceName $NodeExe # 14. 设置 OpenClaw Gateway 启动参数 $AppParameters $OpenClawIndex gateway --port $GatewayPort $nssm set $ServiceName AppParameters $AppParameters $nssm set $ServiceName AppDirectory $OpenClawStateDir $nssm set $ServiceName DisplayName $DisplayName $nssm set $ServiceName Description OpenClaw Gateway background service with auto restart $nssm set $ServiceName Start SERVICE_AUTO_START # 15. 无窗口运行 $nssm set $ServiceName AppNoConsole 1 # 16. 设置服务环境变量 $nssm set $ServiceName AppEnvironmentExtra USERPROFILE$OpenClawUserHome HOME$OpenClawUserHome APPDATA$env:APPDATA LOCALAPPDATA$env:LOCALAPPDATA TEMP$env:TEMP TMP$env:TEMP OPENCLAW_HOME$OpenClawUserHome OPENCLAW_STATE_DIR$OpenClawStateDir OPENCLAW_CONFIG_PATH$OpenClawConfig OPENCLAW_GATEWAY_PORT$GatewayPort PATH$CombinedPath # 17. NSSM 日志重定向 $nssm set $ServiceName AppStdout $LogDir\stdout.log $nssm set $ServiceName AppStderr $LogDir\stderr.log $nssm set $ServiceName AppRotateFiles 1 $nssm set $ServiceName AppRotateOnline 1 $nssm set $ServiceName AppRotateBytes 10485760 # 18. OpenClaw 进程退出后由 NSSM 自动重启 $nssm set $ServiceName AppExit Default Restart $nssm set $ServiceName AppRestartDelay 5000 $nssm set $ServiceName AppThrottle 1500 # 19. Windows 服务自身异常时自动重启 sc.exe failure $ServiceName reset 86400 actions restart/60000/restart/60000/restart/60000 sc.exe failureflag $ServiceName 1 # 20. 启动服务 $nssm start $ServiceName Start-Sleep -Seconds 8 # 21. 验证状态 Get-Service $ServiceName $nssm status $ServiceName $OpenClawCmd gateway status --deep几个关键参数值得单独说明。AppNoConsole 1是无窗口运行的关键没有它服务启动时会弹出一个控制台窗口。AppExit Default Restart配合AppRestartDelay 5000表示进程退出后等 5 秒自动重启AppThrottle 1500是防止疯狂重启的节流。sc.exe failure那两行是 Windows 服务级的恢复策略和 NSSM 的进程级重启形成两层保护OpenClaw Gateway 异常退出 → NSSM 自动重启进程 NSSM 服务异常 → Windows Service Control Manager 自动重启服务日志方面AppRotateFiles 1加AppRotateBytes 10485760表示单文件超过 10MB 就轮转避免日志把磁盘写满。环境变量里把OPENCLAW_STATE_DIR、OPENCLAW_CONFIG_PATH、OPENCLAW_GATEWAY_PORT都显式传进去是为了让服务进程和你在终端里手动跑时看到的是同一套配置不会出现“终端里好好的、服务里读不到配置”的诡异情况。4. 验证服务启动与进程守护是否真的生效脚本跑完最后会打印状态但别只看一眼就完事这一节把验证拆成三步服务状态、Gateway 深度状态、进程守护实测。第一步确认服务在跑Get-Service OpenClawGateway nssm status OpenClawGateway openclaw gateway status --deep正常结果里应该能看到Running、SERVICE_RUNNING、RPC probe: ok、Listening: 127.0.0.1:18789。其中最关键的是RPC probe: ok它表示 Gateway 的 WebSocket 探测已经通过说明服务不只是“进程活着”而是真的能对外提供 RPC 能力。这里会遇到一个容易误判的现象。执行openclaw gateway status --deep时可能看到Service: Scheduled Task (missing) Runtime: unknown RPC probe: okScheduled Task (missing)不代表失败。因为本文方案已经删掉了 OpenClaw 原生计划任务改用 NSSM 托管所以计划任务当然是 missing。真正要关注的是RPC probe: ok、Listening: 127.0.0.1:18789、OpenClawGateway Running、SERVICE_RUNNING这几项。只要它们正常服务就是成功的。第二步浏览器访问 Dashboardhttp://127.0.0.1:18789/页面能打开就说明 Gateway 正常工作。默认绑定回环地址只有本机能访问没必要不要暴露到公网。第三步实测进程守护。注意不要直接taskkill所有node.exe那会误伤其他 Node 程序。正确做法是先精确定位 OpenClaw Gateway 进程$gw Get-CimInstance Win32_Process | Where-Object { $_.CommandLine -like *openclaw*gateway*18789* } | Select-Object -First 1 $gw.ProcessId拿到 PID 后强制结束它Stop-Process -Id $gw.ProcessId -Force Start-Sleep -Seconds 10 nssm status OpenClawGateway openclaw gateway status --deep如果 10 秒后又能看到SERVICE_RUNNING和RPC probe: ok说明 NSSM 已经成功把 Gateway 自动拉起进程守护生效。这一步是整个方案里最值得亲手验证的因为它直接证明“崩了能自己起来”不是嘴上说说。日志排查也顺手记一下。查看 NSSM 的 stdout 和 stderrGet-Content -Encoding UTF8 (Join-Path $env:USERPROFILE .openclaw\logs\service\stdout.log) -Tail 100 Get-Content -Encoding UTF8 (Join-Path $env:USERPROFILE .openclaw\logs\service\stderr.log) -Tail 100 openclaw logs --follow如果 PowerShell 里中文乱码先执行chcp 65001再查看。服务正常启动时日志里通常能看到[gateway] starting HTTP server... [gateway] ready [heartbeat] started [health-monitor] started5. 接入微信渠道与常见报错排查服务稳定之后就可以接微信渠道了。本文用的是openclaw-weixin插件。安装方式有两种任选其一npx -y tencent-weixin/openclaw-weixin-cli install或者用 OpenClaw 插件命令openclaw plugins install tencent-weixin/openclaw-weixin openclaw config set plugins.entries.openclaw-weixin.enabled true因为 Gateway 现在是 NSSM 托管的插件装完必须重启服务才能生效nssm restart OpenClawGateway然后登录微信渠道openclaw channels login --channel openclaw-weixin终端会出现二维码或登录提示用手机微信扫码确认。登录成功后OpenClaw 会把登录状态保存在本地状态目录里。如果要多账号建议设置私聊会话隔离openclaw config set session.dmScope per-account-channel-peer nssm restart OpenClawGateway配对联系人和查看渠道状态openclaw pairing list openclaw-weixin openclaw pairing approve openclaw-weixin PAIRING_CODE openclaw plugins list openclaw channels status --probe正常情况下stdout 日志里能看到[gateway] ready [openclaw-weixin] starting weixin provider [openclaw-weixin] weixin monitor started接下来是排错部分这些都是真实会撞上的报错。报错一把 PowerShell 脚本粘到了 CMD。典型输出是$ServiceName 不是内部或外部命令、New-Item 不是内部或外部命令。这说明当前窗口是 CMD 不是 PowerShell。解决关掉用管理员身份打开 Windows PowerShell 重新执行。报错二NSSM 提示权限不足。报错Administrator access is needed to install a service.或OpenService(): 拒绝访问。原因就是当前 PowerShell 不是管理员。解决关闭窗口重新以管理员身份运行再执行脚本。报错三RPC probe failed。先查端口占用netstat -ano | findstr :18789如果被别的进程占了结束对应 PIDtaskkill /F /PID PID nssm restart OpenClawGateway openclaw gateway status --deep报错四Gateway 配置不完整导致启动失败。先确认本地模式openclaw config set gateway.mode local nssm restart OpenClawGateway报错五微信插件导致 Gateway 反复重启。先临时禁用插件确认 Gateway 本身稳定openclaw config set plugins.entries.openclaw-weixin.enabled false nssm restart OpenClawGateway稳定后再强制更新插件并重新启用openclaw plugins install tencent-weixin/openclaw-weixin --force openclaw config set plugins.entries.openclaw-weixin.enabled true nssm restart OpenClawGateway报错六401 / local proxy failed / reading choices / OAuth 这类接入层错误。这类问题通常出在模型接入配置上和 NSSM 无关。如果你在 OpenClaw 里配置的是兼容 OpenAI 协议的模型服务需要确认三件套齐全Base URL、API Key、Model ID。以 TaoToken 为例Base URL 填https://taotoken.net/apiKey 在控制台生成Model ID 按你实际使用的模型填。配置片段可以写成这样路径以你本机openclaw.json为准{ models: { providers: { taotoken: { baseUrl: https://taotoken.net/api, apiKey: sk-你的Key, model: 你的ModelID } } } }改完配置后重启服务nssm restart OpenClawGateway openclaw gateway status --deep如果报reading choices之类的解析错误多半是 Base URL 少了/api或者 Model ID 写错401一般是 Key 无效或没带上local proxy failed则要检查本机网络和端口是否被拦截。把这三件套对齐接入层错误基本能消掉。6. 长期稳定运行的经验与后续操作整套方案跑通之后日常运维其实就几条命令nssm start OpenClawGateway nssm stop OpenClawGateway nssm restart OpenClawGateway Get-Service OpenClawGateway nssm status OpenClawGateway openclaw gateway status --deep openclaw status openclaw doctor要卸载服务nssm stop OpenClawGateway nssm remove OpenClawGateway confirm安全上有几点必须注意。OpenClaw 状态目录里可能包含配置文件、插件状态、会话数据、登录凭证、微信渠道状态和本地工作区数据不要公开。微信相关的账号 ID、二维码、配对码、token、登录凭证、联系人标识、群聊标识都属于敏感信息别截图外发。Gateway 默认绑定127.0.0.1:18789适合本机访问如果确实需要远程访问建议用带认证的内网方案或反向代理加访问控制不要直接把端口开到公网。另外只保留一种托管方式本文用 NSSM就不要再执行openclaw gateway install否则会重新创建原生计划任务两个 Gateway 抢端口。最终验证清单部署完成后执行Get-Service OpenClawGateway nssm status OpenClawGateway openclaw gateway status --deep openclaw channels status --probe理想状态是OpenClawGateway Running、SERVICE_RUNNING、RPC probe: ok、Listening: 127.0.0.1:18789微信渠道状态正常。这些都满足就说明开机自启、后台无窗口、进程守护、服务宕机自动重启、微信接入全部到位。如果你在接入模型时想先验证 Key 和模型是否可用可以直接用模型对话页面测一下需要生成和管理 Key 就去 API Keys 页面长期跑编码或 Agent 类任务Coding Plan 会更合适。相关入口模型对话https://taotoken.net/chat?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_contentchatAPI Keyshttps://taotoken.net/api-keys?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_contentapikeys接入文档https://taotoken.net/doc?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_contentdocCoding Planhttps://taotoken.net/coding-plan?utm_sourcetaotoken_aicg_blog_endutm_mediumcsdnutm_campaignrewriteutm_contentcodingplan最后留一个我踩过的坑脚本里AppDirectory设成了.openclaw状态目录如果你手动改过 OpenClaw 的工作目录记得同步改这里否则服务启动时读不到相对路径下的配置。改完nssm set OpenClawGateway AppDirectory 你的目录再nssm restart OpenClawGateway即可。
返回列表