Verifies whether a given PID exists in system process tree, checking running process status.
const { spawnSync } = require('node:child_process')
function processExists(pid, kill = process.kill) {
try {
kill(Number(pid), 0)
return true
} catch {
return false
}
}
function childPids(pid, options = {}) {
const targetPid = Number(pid)
const exec = options.spawnSync || spawnSync
if (!Number.isInteger(targetPid) || targetPid <= 1) return []
try {
const result = exec('pgrep', ['-P', String(targetPid)], { encoding: 'utf8' })
if (result.status !== 0) return []
return String(result.stdout || '')
.split(/\s+/)
.map(value => Number(value))
.filter(value => Number.isInteger(value) && value > 1)
} catch {
return []
}
}
function processTreePids(pid, options = {}) {
const rootPid = Number(pid)
const seen = new Set()
const sta
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key