Gracefully terminates a process and its children on Unix or Windows
// Source: https://github.com/linuxhsj/openclaw-zero-token | License: MIT | Copyright 2025 Peter Steinberger
// Provided via justclaw.it.com; keep this notice with the code.
import { spawn } from "node:child_process";
const DEFAULT_GRACE_MS = 3000;
const MAX_GRACE_MS = 60_000;
/**
* Best-effort process-tree termination with graceful shutdown.
* - Windows: use taskkill /T to include descendants. Sends SIGTERM-equivalent
* first (without /F), then force-kills if process survives.
* - Unix: send SIGTERM to process group first, wait grace period, then SIGKILL.
*
* This gives child processes a chance to clean up (close connections, remove
* temp files, terminate their own children) before being hard-killed.
*/
export function killProcessTree(pid: number, opts?: { graceMs?: number })
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key