Windows 下将 Openclaw(ClawdBot) 部署为系统服务

在 Windows 上运行 CLI 工具通常需要一直开着终端窗口,既不美观也不稳定。
本文介绍如何在 Windows Server 或 Windows 10/11 上,使用 Winget 快速配置环境,并利用 NSSM (Non-Sucking Service Manager) 将 openclaw 封装为系统服务。

准备工作

  • 写在前面:clawdbot 命令在旧项目中可用、后来项目变更为moltbot,然后又变更为 openclaw 。安装与命令部分可完全参考官方文档 https://docs.openclaw.ai/start/getting-started
  • 网络环境:需确保能访问 Telegram 服务器。
  • API 准备:获取阿里云百炼或其他兼容 OpenAI/Anthropic 协议的 API Key。

1. 环境安装 (Winget)

我们使用 Windows 自带的包管理器 winget 来快速安装所需软件:NSSM (用于管理服务) 和 Node.js

打开 PowerShell (管理员) 执行:

1
2
3
4
5
# 安装服务管理工具 NSSM
winget install NSSM.NSSM --source winget

# 安装 Node.js (确保版本符合 openclaw 要求)
winget install --id=OpenJS.NodeJS -e --silent --source winget

安装完成后,验证环境:

1
2
3
4
5
# 验证 Node.js 版本
node -v

# 验证 NSSM 是否可用
nssm version

2. 安装 openclaw

环境就绪后,通过 npm 全局安装 openclaw:

1
2
3
4
npm install -g openclaw@latest

# 验证安装
openclaw --version

3. 配置文件

首先需要从 Telegram 的 @BotFather 新建bot获取 botToken

尽可能使用 openclaw onboard 命令初始化配置

系统自动会创建配置文件。
Windows 下通常位于用户目录 C:\Users\用户名\.openclaw\openclaw.json

以下是一份使用阿里云百炼 (通义千问) 兼容 Anthropic 协议的配置示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
"models": {
"providers": {
"anthropic-custom": {
"baseUrl": "https://dashscope.aliyuncs.com/apps/anthropic",
"apiKey": "sk-你的apiKey",
"api": "anthropic-messages",
"models": [
{ "id": "qwen3-coder-plus", "name": "Qwen3 Coder Plus" },
{ "id": "qwen3-coder-flash", "name": "Qwen3 Coder Flash" },
{ "id": "qwen-vl-max", "name": "Qwen VL Max" },
{ "id": "qwen3-max-preview", "name": "qwen3-max-preview" }
]
}
}
},
"agents": {
"defaults": {
"model": {
"primary": "anthropic-custom/qwen3-max-preview"
},
"compaction": {
"mode": "safeguard"
}
}
},
"channels": {
"telegram": {
"enabled": true,
"dmPolicy": "pairing",
"allowFrom": ["你的telegram_id"],
"botToken": "你的_TG_BOT_TOKEN",
"groupPolicy": "allowlist",
"streamMode": "partial",
"proxy": "http://127.0.0.1:7897"
}
},
"gateway": {
"mode": "local"
},
"plugins": {
"entries": {
"telegram": {
"enabled": true
}
}
}
}

提示:如果有代理需求,请在 channels.telegram 中正确配置 proxy 字段。

4. 首次运行与配对

先在命令行手动运行,完成配对流程:

1
openclaw gateway --verbose

在 Telegram 向你的 Bot 发送消息,终端会显示配对码:

1
2
Pairing code: XXXXXX
Your Telegram user id: 123456789

新建一个终端窗口,执行配对命令:

1
openclaw pairing approve telegram XXXXXX

配对成功后,接下来我们将其配置为服务。

5. 配置 Windows 服务 (核心步骤)

openclaw 自带了一个 daemon install 命令,它会生成一个启动脚本,我们可以利用它:

1
openclaw daemon install --force

输出示例:

Installed Scheduled Task: openclaw Gateway
Task script: C:\Users\Admin.openclaw\gateway.cmd

虽然它自动创建了计划任务,但为了更稳定的管理,我们使用 NSSM 将其包装为标准服务。

5.1 创建服务

使用刚才生成的 gateway.cmd 作为服务入口:

1
2
$ClawHome = "$env:USERPROFILE\.openclaw"
nssm install openclaw "$ClawHome\gateway.cmd"

NSSM 创建服务

5.2 配置路径与日志

为了方便排错,我们需要重定向标准输出和错误输出:

1
2
3
4
5
6
7

# 设置日志路径
nssm set openclaw AppStdout "$ClawHome\logs\out.log"
nssm set openclaw AppStderr "$ClawHome\logs\out.log"

# 设置自动启动
nssm set openclaw Start SERVICE_AUTO_START

5.3 启动服务与排错

尝试启动服务:

1
nssm start openclaw

🚨 常见问题:

配置加载失败

如果启动后日志报错 Missing config. Run openclaw setup...,这是因为默认服务运行在 LocalSystem 账户下,找不到你当前用户目录下的 .openclaw 配置文件。

解决方法:

修改服务的登录账户为你当前的用户。

服务卡在 STOP_PENDING 状态,无法响应新的控制命令。

这通常发生在:

  • 服务停止过程中出错/卡住
  • 子进程(node.exe/cmd.exe)没有正常退出
  • NSSM 服务管理器等待超时

解决步骤:

  1. sc query openclaw - 查看服务状态
  2. 找到服务对应的进程 PID
  3. taskkill /F /PID - 强制结束卡住的进程
  4. nssm start openclaw - 重新启动服务

以后遇到类似问题可以快速处理:

查找服务PID并强制结束

powershell -Command “taskkill /F /PID (Get-WmiObject Win32_Service -Filter
"Name=’openclaw’").ProcessId”

然后重启

nssm start openclaw

1
2
# 打开 GUI 编辑界面
nssm edit openclaw
  1. 切换到 Log on (登录) 选项卡。
  2. 选择 This account (此账户)。
  3. 输入你的 Windows 用户名和密码。
  4. 点击 Edit service 保存。

NSSM 登录账户设置

最后重启服务:

1
nssm restart openclaw

服务成功运行

现在,openclaw 就会作为 Windows 服务在后台静默运行了。

运行状态验证

部署完成后,你可以在 Telegram 中通过相关命令查看 Bot 的运行状态,确保其正确识别了 Windows 环境和模型:

Windows 运行状态信息

常用管理命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 停止服务
nssm stop openclaw

# 删除服务
nssm remove openclaw

# 查看状态
Get-Service openclaw

nssm status openclaw
nssm dump openclaw

# 查看实时日志( gateway.cmd 文件中的命令行后追加 --verbose 可查看细节)
Get-Content "$ClawHome\logs\out.log" -Wait -Tail 20 -Encoding utf8