openclaw/src/shared/pid-alive.ts

34 lines
758 B
TypeScript

import fsSync from "node:fs";
/**
* Check if a process is a zombie on Linux by reading /proc/<pid>/status.
* Returns false on non-Linux platforms or if the proc file can't be read.
*/
function isZombieProcess(pid: number): boolean {
if (process.platform !== "linux") {
return false;
}
try {
const status = fsSync.readFileSync(`/proc/${pid}/status`, "utf8");
const stateMatch = status.match(/^State:\s+(\S)/m);
return stateMatch?.[1] === "Z";
} catch {
return false;
}
}
export function isPidAlive(pid: number): boolean {
if (!Number.isFinite(pid) || pid <= 0) {
return false;
}
try {
process.kill(pid, 0);
} catch {
return false;
}
if (isZombieProcess(pid)) {
return false;
}
return true;
}