Files
ts-scripts/libraries/deno-process-mod.ts
2026-02-08 16:04:51 +08:00

74 lines
1.9 KiB
TypeScript

export class Process {
user: string;
pid: number;
cpu: number;
mem: number;
vsz: number;
rss: number;
tty: string;
stat: string;
start: string;
time: string;
command: string;
constructor(
user: string,
pid: number,
cpu: number,
mem: number,
vsz: number,
rss: number,
tty: string,
stat: string,
start: string,
time: string,
command: string,
) {
this.user = user;
this.pid = pid;
this.cpu = cpu;
this.mem = mem;
this.vsz = vsz;
this.rss = rss;
this.tty = tty;
this.stat = stat;
this.start = start;
this.time = time;
this.command = command;
}
}
export function parseProcessLine(line: string): Process | null {
const processMatchRegex =
/^\s*([\w+\-_]+)\s+(\d+)\s+([\d.]+)\s+([\d.]+)\s+(\d+)\s+(\d+)\s+([\w\/\\?]+)\s+([\w+<>]+)\s+([\w:]+)\s+([\d:]+)\s+(.*)$/;
// "app 3622 0.2 24.0 2932504 453004 ? Sl Jan25 23:04 /usr/lib/jvm/jdk-25/bin/java -Dfastjson.parser.safeMode=true......";
// USER PID CPU MEM VSZ RSS TTY STAT START TIME COMMAND
const matcher = line.match(processMatchRegex);
if (!matcher) {
return null;
}
const user = matcher[1];
const pid = parseInt(matcher[2]);
const cpu = Number(matcher[3]);
const mem = Number(matcher[4]);
const vsz = parseInt(matcher[5]);
const rss = parseInt(matcher[6]);
const tty = matcher[7];
const stat = matcher[8];
const start = matcher[9];
const time = matcher[10];
const command = matcher[11];
return new Process(
user,
pid,
cpu,
mem,
vsz,
rss,
tty,
stat,
start,
time,
command,
);
}