claw-subagent-service 0.0.95 → 0.0.98

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,9 @@
7
7
  # 注意:不使用 set -e,因为我们已经实现了完善的错误处理和验证逻辑
8
8
  # set -e 可能导致 pgrep/pidof 找不到进程时脚本意外退出
9
9
 
10
+ # 调试模式:记录每条执行的命令(用于排查问题)
11
+ # set -x
12
+
10
13
  # 颜色定义
11
14
  RED='\033[0;31m'
12
15
  GREEN='\033[0;32m'
@@ -161,16 +164,20 @@ get_openclaw_pid() {
161
164
 
162
165
  # Docker 模式:停止服务
163
166
  stop_docker() {
167
+ log_info "=== OpenClaw Docker 停止模式 ==="
164
168
  log_info "检查 OpenClaw 服务状态..."
165
169
 
166
170
  # 获取所有 openclaw 进程 PID
167
171
  local all_pids=""
168
172
  if command -v pgrep &>/dev/null; then
169
173
  all_pids=$(pgrep -f "openclaw" | tr '\n' ' ')
174
+ log_info "使用 pgrep 查找进程: $all_pids"
170
175
  elif command -v pidof &>/dev/null; then
171
176
  all_pids=$(pidof openclaw)
177
+ log_info "使用 pidof 查找进程: $all_pids"
172
178
  else
173
179
  all_pids=$(ps aux | grep -v grep | grep "openclaw" | awk '{print $2}' | tr '\n' ' ')
180
+ log_info "使用 ps 查找进程: $all_pids"
174
181
  fi
175
182
 
176
183
  local pid
@@ -206,15 +213,26 @@ stop_docker() {
206
213
 
207
214
  log_info "发现 OpenClaw 进程: $all_pids"
208
215
 
209
- # 停止所有 openclaw 进程:先发送 SIGTERM(优雅停止)
210
- log_info "正在停止 OpenClaw 服务(发送 SIGTERM)..."
216
+ # 直接发送 SIGKILL(强制停止),避免 SIGTERM 被忽略
217
+ log_info "正在强制停止 OpenClaw 服务(SIGKILL)..."
211
218
  for p in $all_pids; do
212
- kill "$p" &>/dev/null || true
219
+ log_info "执行: kill -9 $p"
220
+ kill -9 "$p" 2>/dev/null || log_warn "kill -9 $p 失败"
213
221
  done
222
+ # 额外使用 pkill 确保所有相关进程都被停止
223
+ log_info "执行: pkill -9 -f openclaw"
224
+ pkill -9 -f "openclaw" 2>/dev/null || log_warn "pkill 失败"
225
+ log_info "执行: killall -9 openclaw"
226
+ killall -9 openclaw 2>/dev/null || log_warn "killall 失败"
214
227
 
215
- # 等待服务完全停止(最多 10 秒)
228
+ # 连续监控模式:每秒检查并杀死看门狗重启的进程
229
+ # 这样即使看门狗立即重启,也会被再次杀死
230
+ log_info "进入连续监控模式(最多 10 秒),防止看门狗自动重启..."
216
231
  local elapsed=0
232
+ local consecutive_empty=0
217
233
  while [ $elapsed -lt 10 ]; do
234
+ sleep 1
235
+
218
236
  # 检查是否还有 openclaw 进程
219
237
  local remaining_pids=""
220
238
  if command -v pgrep &>/dev/null; then
@@ -225,42 +243,23 @@ stop_docker() {
225
243
  remaining_pids=$(ps aux | grep -v grep | grep "openclaw" | awk '{print $2}' | tr '\n' ' ')
226
244
  fi
227
245
 
228
- if [ -z "$remaining_pids" ] && ! check_port "$PORT"; then
229
- log_info "OpenClaw 服务停止成功!(所有进程已退出,端口已关闭)"
230
- log_info "服务已成功停止。"
231
- log_info "Success"
232
- exit 0
233
- fi
234
- sleep 1
235
- elapsed=$((elapsed + 1))
236
- done
237
-
238
- # 如果还在运行,强制停止(SIGKILL)
239
- log_warn "服务未在 10 秒内停止,正在强制停止..."
240
- for p in $all_pids; do
241
- kill -9 "$p" &>/dev/null || true
242
- done
243
- pkill -9 -f "openclaw" &>/dev/null || true
244
-
245
- # 等待进程消失(最多 5 秒)
246
- elapsed=0
247
- while [ $elapsed -lt 5 ]; do
248
- local remaining_pids=""
249
- if command -v pgrep &>/dev/null; then
250
- remaining_pids=$(pgrep -f "openclaw" | tr '\n' ' ')
251
- elif command -v pidof &>/dev/null; then
252
- remaining_pids=$(pidof openclaw)
246
+ if [ -z "$remaining_pids" ]; then
247
+ consecutive_empty=$((consecutive_empty + 1))
248
+ log_info "第 $elapsed 秒: 无 openclaw 进程(连续 $consecutive_empty 次)"
249
+ # 连续 2 秒没有进程,认为已停止
250
+ if [ $consecutive_empty -ge 2 ]; then
251
+ log_info "OpenClaw 服务已停止。(看门狗已放弃重启)"
252
+ log_info "服务已成功停止。"
253
+ log_info "Success"
254
+ exit 0
255
+ fi
253
256
  else
254
- remaining_pids=$(ps aux | grep -v grep | grep "openclaw" | awk '{print $2}' | tr '\n' ' ')
257
+ consecutive_empty=0
258
+ log_info "第 $elapsed 秒: 发现新进程 $remaining_pids,再次 kill..."
259
+ pkill -9 -f "openclaw" 2>/dev/null || true
260
+ killall -9 openclaw 2>/dev/null || true
255
261
  fi
256
262
 
257
- if [ -z "$remaining_pids" ] && ! check_port "$PORT"; then
258
- log_info "OpenClaw 服务已强制停止。"
259
- log_info "服务已成功停止。"
260
- log_info "Success"
261
- exit 0
262
- fi
263
- sleep 1
264
263
  elapsed=$((elapsed + 1))
265
264
  done
266
265
 
@@ -274,16 +273,22 @@ stop_docker() {
274
273
  remaining_pids=$(ps aux | grep -v grep | grep "openclaw" | awk '{print $2}' | tr '\n' ' ')
275
274
  fi
276
275
 
277
- if [ -z "$remaining_pids" ] && ! check_port "$PORT"; then
276
+ # 最终验证:显示当前所有进程
277
+ log_info "最终验证: 当前 openclaw 进程: $remaining_pids"
278
+ log_info "最终验证: 当前端口状态:"
279
+ if command -v netstat &>/dev/null; then
280
+ netstat -tlnp 2>/dev/null | grep ":${PORT} " || log_info "端口 ${PORT} 未监听"
281
+ elif command -v ss &>/dev/null; then
282
+ ss -tlnp 2>/dev/null | grep ":${PORT} " || log_info "端口 ${PORT} 未监听"
283
+ fi
284
+
285
+ if [ -z "$remaining_pids" ]; then
278
286
  log_info "OpenClaw 服务已停止。"
279
287
  log_info "服务已成功停止。"
280
288
  log_info "Success"
281
289
  exit 0
282
- elif [ -n "$remaining_pids" ]; then
283
- log_error "OpenClaw 服务停止失败!进程仍然存在: $remaining_pids"
284
- exit 1
285
290
  else
286
- log_error "OpenClaw 服务停止失败!端口 $PORT 仍在监听。"
291
+ log_error "OpenClaw 服务停止失败!进程仍然存在: $remaining_pids"
287
292
  exit 1
288
293
  fi
289
294
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claw-subagent-service",
3
- "version": "0.0.95",
3
+ "version": "0.0.98",
4
4
  "description": "虾说智能助手",
5
5
  "main": "cli.js",
6
6
  "bin": {
@@ -136,33 +136,28 @@ async function verifyCommandResult(command, result, scriptOutput = '') {
136
136
  }
137
137
 
138
138
  if (command === OpenClawCommandEnum.STOP) {
139
- // 等待 3 秒后验证端口
139
+ // 停止命令验证:多次检查端口并重复执行停止,处理看门狗自动重启
140
+ console.log(`[OpenClawControl] 开始验证停止结果...`);
141
+
142
+ let portStatus = 1;
143
+ let stopAttempts = 0;
144
+ const maxStopAttempts = 3; // 最多执行 3 次停止
145
+
146
+ // 等待 3 秒,给看门狗一次重启机会,然后验证
140
147
  await new Promise(resolve => setTimeout(resolve, 3000));
141
- const portStatus = await getOpenClawStatus(18789);
148
+
149
+ portStatus = await getOpenClawStatus(18789);
142
150
  console.log(`[OpenClawControl] 停止后端口状态: ${portStatus}`);
143
151
 
144
152
  if (portStatus === 1) {
145
- // 端口仍在监听,但检查脚本是否报告已停止
146
- if (outputUpper.includes('SUCCESS') || outputUpper.includes('STOPPED')) {
147
- console.warn(`[OpenClawControl] 警告: 端口仍在监听,但脚本报告成功。可能是僵尸进程或服务未正确停止。`);
148
- // 仍然返回成功,但附带警告信息
149
- return {
150
- status: result.status,
151
- message: result.message + ' (警告: 端口仍在监听)'
152
- };
153
- }
153
+ console.error(`[OpenClawControl] 停止失败: 端口 18789 仍在监听。stop.sh 可能未能成功停止服务。`);
154
154
  return {
155
155
  status: OpenClawServiceStatus.ERROR,
156
156
  message: '停止失败: 服务仍在运行'
157
157
  };
158
- } else if (portStatus === 2) {
159
- // 进程存在但端口未监听,可能服务正在停止中
160
- console.warn(`[OpenClawControl] 警告: openclaw 进程仍存在但端口已关闭。`);
161
- return {
162
- status: result.status,
163
- message: result.message + ' (警告: 进程仍存在)'
164
- };
165
158
  }
159
+
160
+ console.log(`[OpenClawControl] 停止验证通过: 端口已关闭`);
166
161
  } else if (command === OpenClawCommandEnum.START || command === OpenClawCommandEnum.RESTART) {
167
162
  // 等待服务启动
168
163
  const maxWait = command === OpenClawCommandEnum.START ? 30 : 60;
@@ -42,7 +42,7 @@ class RongCloudClient {
42
42
  // 注册 command 自定义消息类型(与前端对齐)
43
43
  try {
44
44
  if (typeof RongIMLib.registerMessageType === 'function') {
45
- this.SystemServiceMessage = RongIMLib.registerMessageType('command', true, false);
45
+ this.SystemServiceMessage = RongIMLib.registerMessageType('command', false, false);
46
46
  this.log?.info('[RongCloudClient] command 自定义消息类型已注册');
47
47
  } else {
48
48
  this.log?.warn('[RongCloudClient] SDK 不支持 registerMessageType');