@fefeding/aicmd 1.0.19 → 1.0.21

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.
Files changed (2) hide show
  1. package/bin/aicmd.js +59 -12
  2. package/package.json +1 -1
package/bin/aicmd.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  const crypto = require('crypto');
4
- const { exec, spawn } = require('child_process');
4
+ const { exec, spawn, execSync } = require('child_process');
5
5
  const path = require('path');
6
6
  const fs = require('fs');
7
7
  const net = require('net');
@@ -176,22 +176,69 @@ async function startProject() {
176
176
 
177
177
  function stopProject() {
178
178
  console.log('Stopping aicmd...');
179
+
180
+ // 解析 -p / --port 参数(与 start 保持一致)
181
+ let port = 9801;
182
+ const portIndex = commandArgs.findIndex(arg => arg === '-p' || arg === '--port');
183
+ if (portIndex !== -1 && commandArgs[portIndex + 1]) {
184
+ const p = parseInt(commandArgs[portIndex + 1]);
185
+ if (!isNaN(p)) port = p;
186
+ }
187
+
188
+ let stopped = false;
189
+
190
+ // 1) 优先用 PID 文件
179
191
  const pid = readPid();
180
- if (!pid) {
181
- console.log('No server process found');
182
- return;
192
+ if (pid) {
193
+ try {
194
+ process.kill(pid, 'SIGTERM');
195
+ console.log('Stopped process from PID file:', pid);
196
+ stopped = true;
197
+ } catch (error) {
198
+ if (error.code !== 'ESRCH') {
199
+ console.error('Failed to stop PID', pid + ':', error.message);
200
+ }
201
+ }
183
202
  }
184
203
 
185
- try {
186
- process.kill(pid, 'SIGTERM');
187
- deletePid();
188
- console.log('Server stopped');
189
- } catch (error) {
190
- if (error.code === 'ESRCH') {
191
- deletePid();
204
+ // 2) 兜底:查找监听该端口的 node server.js 进程(PID 文件丢失/跨用户/进程已变均适用)
205
+ const portPids = findPidsByPort(port);
206
+ for (const p of portPids) {
207
+ try {
208
+ process.kill(p, 'SIGTERM');
209
+ console.log('Stopped process on port', port + ':', p);
210
+ stopped = true;
211
+ } catch (error) {
212
+ if (error.code !== 'ESRCH') {
213
+ console.error('Failed to stop PID', p + ':', error.message);
214
+ }
192
215
  }
193
- console.error('Failed to stop:', error.message);
194
216
  }
217
+
218
+ // 3) 清理所有已知位置的 PID 文件(root 与普通用户),避免跨用户遗留
219
+ deletePid();
220
+ try { fs.unlinkSync('/root/.aicmd/aicmd.server.pid'); } catch { /* ignore */ }
221
+
222
+ if (stopped) {
223
+ console.log('Server stopped');
224
+ } else {
225
+ console.log('No running aicmd server found on port', port);
226
+ }
227
+ }
228
+
229
+ // 通过 lsof 查找监听指定端口的进程 PID 列表(macOS / Linux 通用)
230
+ function findPidsByPort(port) {
231
+ const pids = new Set();
232
+ try {
233
+ const out = execSync(`lsof -tiTCP:${port} -sTCP:LISTEN 2>/dev/null`).toString().trim();
234
+ if (out) {
235
+ for (const line of out.split('\n')) {
236
+ const p = parseInt(line.trim());
237
+ if (!isNaN(p)) pids.add(p);
238
+ }
239
+ }
240
+ } catch { /* lsof 不可用或无可匹配进程 */ }
241
+ return [...pids];
195
242
  }
196
243
 
197
244
  function restartProject() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fefeding/aicmd",
3
- "version": "1.0.19",
3
+ "version": "1.0.21",
4
4
  "private": false,
5
5
  "type": "commonjs",
6
6
  "description": "AI-powered web SSH terminal with autonomous agent, skills, and file transfer",