claw-subagent-service 0.0.137 → 0.0.139

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.
@@ -184,11 +184,19 @@ restart_docker() {
184
184
  log_warn "服务未运行"
185
185
  fi
186
186
 
187
- # 如果正在运行,先停止
187
+ # 如果正在运行,先停止(包括进程存在但端口未监听的情况)
188
188
  if [ -n "$pid" ] || check_port "$PORT"; then
189
189
  log_info "正在停止 OpenClaw 服务..."
190
190
  if [ -n "$pid" ]; then
191
- kill "$pid" &>/dev/null || true
191
+ log_info "停止进程 $pid..."
192
+ kill -15 "$pid" &>/dev/null || true
193
+ sleep 2
194
+ # 检查是否还在运行
195
+ if ps -p "$pid" > /dev/null 2>&1; then
196
+ log_warn "进程 $pid 仍在运行,强制停止..."
197
+ kill -9 "$pid" &>/dev/null || true
198
+ sleep 1
199
+ fi
192
200
  fi
193
201
 
194
202
  # 等待停止(最多 10 秒),使用端口双重验证
@@ -204,10 +212,12 @@ restart_docker() {
204
212
  done
205
213
 
206
214
  # 如果还在运行,强制停止并使用备选方案
207
- if check_port "$PORT"; then
215
+ local current_pid_after_wait
216
+ current_pid_after_wait=$(get_openclaw_pid)
217
+ if [ -n "$current_pid_after_wait" ] || check_port "$PORT"; then
208
218
  log_warn "服务未在 10 秒内停止,正在强制停止..."
209
- if [ -n "$pid" ]; then
210
- kill -9 "$pid" &>/dev/null || true
219
+ if [ -n "$current_pid_after_wait" ]; then
220
+ kill -9 "$current_pid_after_wait" &>/dev/null || true
211
221
  fi
212
222
  pkill -9 -f "openclaw" &>/dev/null || true
213
223
  if command -v fuser &>/dev/null; then
@@ -216,8 +226,10 @@ restart_docker() {
216
226
  sleep 2
217
227
 
218
228
  # 最终验证
219
- if check_port "$PORT"; then
220
- log_error "OpenClaw 服务停止失败!端口 $PORT 仍在监听。"
229
+ local final_pid
230
+ final_pid=$(get_openclaw_pid)
231
+ if [ -n "$final_pid" ] || check_port "$PORT"; then
232
+ log_error "OpenClaw 服务停止失败!进程或端口仍在运行。"
221
233
  exit 1
222
234
  fi
223
235
  fi
@@ -240,8 +252,16 @@ restart_docker() {
240
252
 
241
253
  log_info "正在启动 OpenClaw 服务..."
242
254
 
243
- # 使用 nohup 后台启动
244
- nohup openclaw gateway --port "$PORT" > "$log_file" 2>&1 &
255
+ # 使用 setsid 创建新会话,完全脱离父进程
256
+ # 这样即使父进程(Node.js)退出,openclaw 也不会被终止
257
+ log_info "使用 setsid 启动,确保进程脱离父进程..."
258
+ if command -v setsid &>/dev/null; then
259
+ setsid bash -c "openclaw gateway run --port $PORT" > "$log_file" 2>&1 &
260
+ else
261
+ # 如果没有 setsid,使用 nohup 作为后备
262
+ log_warn "setsid 不可用,使用 nohup 作为后备..."
263
+ nohup openclaw gateway run --port "$PORT" > "$log_file" 2>&1 &
264
+ fi
245
265
 
246
266
  log_info "OpenClaw 服务启动命令已发送(PID: $!)"
247
267
  log_info "日志文件: $log_file"
@@ -186,12 +186,25 @@ start_docker() {
186
186
  local pid
187
187
  pid=$(get_openclaw_pid)
188
188
  if [ -n "$pid" ]; then
189
- log_info "OpenClaw 服务已经在运行中。"
189
+ log_info "检测到 openclaw 进程 (PID: $pid)"
190
190
  if check_port "$PORT"; then
191
+ log_info "OpenClaw 服务已经在运行中。"
191
192
  log_info "控制界面访问地址: http://127.0.0.1:$PORT/"
193
+ log_info "Success"
194
+ exit 0
195
+ else
196
+ log_warn "进程存在但端口 $PORT 未监听,进程可能未正确启动或已崩溃"
197
+ log_warn "将停止现有进程并重新启动..."
198
+ # 停止现有进程
199
+ kill -15 "$pid" 2>/dev/null || true
200
+ sleep 2
201
+ # 检查是否还在运行
202
+ if ps -p "$pid" > /dev/null 2>&1; then
203
+ log_warn "进程仍在运行,强制停止..."
204
+ kill -9 "$pid" 2>/dev/null || true
205
+ sleep 1
206
+ fi
192
207
  fi
193
- log_info "Success"
194
- exit 0
195
208
  fi
196
209
 
197
210
  # 检查 openclaw 命令是否存在
@@ -288,13 +301,21 @@ start_docker() {
288
301
  # 注意:openclaw gateway 可能使用不同的参数名
289
302
  log_info "尝试启动: openclaw gateway run --port $PORT"
290
303
 
291
- # 使用 nohup 后台启动,将输出重定向到日志文件
292
- nohup openclaw gateway run --port "$PORT" > "$log_file" 2>&1 &
304
+ # 使用 setsid 创建新会话,完全脱离父进程
305
+ # 这样即使父进程(Node.js)退出,openclaw 也不会被终止
306
+ log_info "使用 setsid 启动,确保进程脱离父进程..."
307
+ if command -v setsid &>/dev/null; then
308
+ setsid bash -c "openclaw gateway run --port $PORT" > "$log_file" 2>&1 &
309
+ else
310
+ # 如果没有 setsid,使用 nohup 作为后备
311
+ log_warn "setsid 不可用,使用 nohup 作为后备..."
312
+ nohup openclaw gateway run --port "$PORT" > "$log_file" 2>&1 &
313
+ fi
314
+ local started_pid=$!
315
+ log_info "启动的进程 PID: $started_pid"
293
316
 
294
317
  # 等待 15 秒检查进程是否启动(给更多时间初始化)
295
318
  sleep 15
296
- local started_pid=$!
297
- log_info "启动的进程 PID: $started_pid"
298
319
 
299
320
  # 检查进程是否存在
300
321
  if ! ps -p "$started_pid" > /dev/null 2>&1; then
@@ -204,11 +204,24 @@ stop_docker() {
204
204
  fi
205
205
  fi
206
206
 
207
- if ! check_port "$PORT" && [ -z "$(ps aux | grep -v grep | grep 'openclaw' | awk '{print $2}')" ]; then
207
+ # 再次检查进程和端口
208
+ local final_check_pid
209
+ final_check_pid=$(get_openclaw_pid)
210
+ local final_check_port=false
211
+ if check_port "$PORT"; then
212
+ final_check_port=true
213
+ fi
214
+
215
+ if [ -z "$final_check_pid" ] && [ "$final_check_port" = false ]; then
208
216
  log_warn "OpenClaw 服务未在运行。"
209
217
  exit 0
210
218
  else
211
- log_error "OpenClaw 服务停止失败!"
219
+ if [ -n "$final_check_pid" ]; then
220
+ log_error "OpenClaw 服务停止失败!进程仍然存在: $final_check_pid"
221
+ fi
222
+ if [ "$final_check_port" = true ]; then
223
+ log_error "OpenClaw 服务停止失败!端口 $PORT 仍在监听。"
224
+ fi
212
225
  exit 1
213
226
  fi
214
227
  fi
@@ -373,6 +386,31 @@ stop_docker() {
373
386
  fi
374
387
  }
375
388
 
389
+ # Systemd 模式:停止服务
390
+ # 参数: $1 = force (1=强制停止, 空=优雅停止)
391
+ stop_systemd() {
392
+ local force="$1"
393
+
394
+ # 检查服务是否存在
395
+ if ! systemctl --user list-unit-files "$SERVICE_NAME" &>/dev/null; then
396
+ log_error "服务 $SERVICE_NAME 不存在。"
397
+ exit 1
398
+ fi
399
+
400
+ # 检查服务状态
401
+ log_info "检查 OpenClaw 服务状态..."
402
+ if systemctl --user is-active --quiet "$SERVICE_NAME"; then
403
+ log_info "OpenClaw 服务正在运行,准备停止..."
404
+ else
405
+ log_warn "OpenClaw 服务未在运行。"
406
+ exit 0
407
+ fi
408
+
409
+ # 停止服务
410
+ log_info "正在停止 OpenClaw 服务..."
411
+
412
+ if systemctl --user stop "$SERVICE_NAME"; then
413
+
376
414
  # Systemd 模式:停止服务
377
415
  # 参数: $1 = force (1=强制停止, 空=优雅停止)
378
416
  stop_systemd() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claw-subagent-service",
3
- "version": "0.0.137",
3
+ "version": "0.0.139",
4
4
  "description": "虾说智能助手",
5
5
  "main": "cli.js",
6
6
  "bin": {
@@ -59,6 +59,14 @@ function fixShellScriptLineEndings() {
59
59
  fixedCount++;
60
60
  console.log(`[postinstall] 已修复 ${file} 换行符(CRLF → LF)`);
61
61
  }
62
+ // 确保脚本有执行权限
63
+ try {
64
+ fs.accessSync(filePath, fs.constants.X_OK);
65
+ } catch (e) {
66
+ // 没有执行权限,添加
67
+ fs.chmodSync(filePath, 0o755);
68
+ console.log(`[postinstall] 已添加 ${file} 执行权限`);
69
+ }
62
70
  } catch (e) {
63
71
  console.warn(`[postinstall] 修复 ${file} 失败: ${e.message}`);
64
72
  }
@@ -247,10 +247,17 @@ class ScriptExecutor {
247
247
  console.log(`[ScriptExecutor-DEBUG] SHELL: ${process.env.SHELL}`);
248
248
  }
249
249
 
250
- const child = spawn(cmd, args, {
251
- detached: false,
250
+ // 对于启动脚本,使用 detached: true 允许子进程脱离父进程
251
+ // 这样 openclaw 进程不会在脚本结束后被终止
252
+ const isStartScript = scriptPath.includes('start');
253
+ const spawnOptions = {
254
+ detached: isStartScript, // 启动脚本使用 detached 模式
252
255
  windowsHide: true
253
- });
256
+ };
257
+
258
+ console.log(`[ScriptExecutor] spawn 选项: detached=${spawnOptions.detached}`);
259
+
260
+ const child = spawn(cmd, args, spawnOptions);
254
261
 
255
262
  let stdout = '';
256
263
  let stderr = '';