-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.js
More file actions
80 lines (76 loc) · 2.68 KB
/
process.js
File metadata and controls
80 lines (76 loc) · 2.68 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { shellRun as run, shellEscape } from '../utils/shell.js';
import { getLogger } from '../utils/logger.js';
export const definitions = [
{
name: 'process_list',
description: 'List running processes. Optionally filter by name.',
input_schema: {
type: 'object',
properties: {
filter: { type: 'string', description: 'Filter processes by name (optional)' },
},
},
},
{
name: 'kill_process',
description: 'Kill a process by PID or name.',
input_schema: {
type: 'object',
properties: {
pid: { type: 'number', description: 'Process ID to kill' },
name: { type: 'string', description: 'Process name to kill (uses pkill)' },
},
},
},
{
name: 'service_control',
description: 'Manage systemd services (start, stop, restart, status).',
input_schema: {
type: 'object',
properties: {
service: { type: 'string', description: 'Service name' },
action: {
type: 'string',
description: 'Action to perform',
enum: ['start', 'stop', 'restart', 'status'],
},
},
required: ['service', 'action'],
},
},
];
export const handlers = {
process_list: async (params) => {
const logger = getLogger();
const filter = params.filter;
logger.debug(`process_list: ${filter ? `filtering by "${filter}"` : 'listing all'}`);
const cmd = filter ? `ps aux | head -1 && ps aux | grep -i ${shellEscape(filter)} | grep -v grep` : 'ps aux';
return await run(cmd);
},
kill_process: async (params) => {
const logger = getLogger();
if (params.pid) {
const pid = parseInt(params.pid, 10);
if (!Number.isFinite(pid) || pid <= 0) return { error: 'Invalid PID' };
logger.debug(`kill_process: killing PID ${pid}`);
const result = await run(`kill ${pid}`);
if (result.error) logger.error(`kill_process failed for PID ${pid}: ${result.error}`);
return result;
}
if (params.name) {
logger.debug(`kill_process: killing processes matching "${params.name}"`);
const result = await run(`pkill -f ${shellEscape(params.name)}`);
if (result.error) logger.error(`kill_process failed for name "${params.name}": ${result.error}`);
return result;
}
return { error: 'Provide either pid or name' };
},
service_control: async (params) => {
const logger = getLogger();
const { service, action } = params;
logger.debug(`service_control: ${action} ${service}`);
const result = await run(`systemctl ${shellEscape(action)} ${shellEscape(service)}`);
if (result.error) logger.error(`service_control failed: ${action} ${service}: ${result.error}`);
return result;
},
};