You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
34 lines
758 B
TypeScript
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;
|
|
}
|