npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

endtree

v1.2.0

Published

Kill a process and all its descendants on Linux, macOS and Windows. Promise API, waits for exit, escalates to SIGKILL, no shell strings. A safer tree-kill. Zero dependencies.

Readme

endtree

Kill a process and every process it started, on Linux, macOS and Windows. Waits until they are really gone.

npm version bundle size license

When you kill a process, the processes it started do not stop with it. For example, npm run dev starts a shell, and the shell starts your dev server. If you kill only npm, the server keeps running as an orphan (a process whose parent is gone) and keeps its port busy. endtree finds the whole process tree, signals every process in it, and resolves when all of them have exited. Use it in place of tree-kill.

Features

  • Promise API: resolves when the whole tree has exited, with a report of what was signalled.
  • Escalates: a process that ignores SIGTERM gets SIGKILL after a timeout (default 5 seconds).
  • Does not miss processes: freezes the tree before signalling, so children started during the kill and detached children are found too.
  • Safe: never runs a shell (argument arrays only), the pid must be a real integer, and reused pids are detected.
  • Fast on Linux: reads /proc directly and starts no ps processes.
  • List without killing: descendants(pid) and processTree(pid).
  • Clear errors: EPERM (not allowed), ETIMEDOUT (still running) and more, with the pids involved.
  • Zero dependencies, ESM and CommonJS, TypeScript types included.

Installation

npm install endtree
pnpm add endtree
yarn add endtree
bun add endtree

Quick start

import endtree, { descendants } from 'endtree'
import { spawn } from 'node:child_process'
import { setTimeout as sleep } from 'node:timers/promises'

// A shell that starts two `sleep` processes
const child = spawn('sh', ['-c', 'sleep 60 & sleep 60; wait'])
await sleep(100) // give the shell time to start them

await descendants(child.pid)  // => [5121, 5122]  (the two `sleep` processes)

const result = await endtree(child.pid)
result.signalled              // => [5122, 5121, 5120]  (deepest first, the shell last)
result.killed                 // => []  (all exited after SIGTERM, no SIGKILL needed)

await descendants(child.pid)  // => []  (everything is gone)

The pids in this README are examples. Yours will be different.

CommonJS works too: const endtree = require('endtree').

Table of contents

Usage

Stop a command and everything it started

child.kill() signals only the process you spawned. Its children keep running:

import { spawn } from 'node:child_process'

const dev = spawn('npm', ['run', 'dev'])

dev.kill()
// npm exits, but the dev server it started (through a shell) is still running.

Use endtree instead. It signals npm, the shell, the dev server and all of their children, and resolves once they have exited:

import endtree from 'endtree'
import { spawn } from 'node:child_process'

const dev = spawn('npm', ['run', 'dev'])

// Later, when you want to stop it:
await endtree(dev.pid)

Give processes more or less time

By default, endtree sends SIGTERM, waits up to 5 seconds, and then sends SIGKILL to anything still running.

await endtree(pid, { signal: 'SIGINT', timeout: 10_000 })  // like Ctrl+C, wait up to 10 s
await endtree(pid, { signal: 'SIGKILL' })                  // stop at once, no waiting period
await endtree(pid, { timeout: Infinity })                  // wait as long as it takes

The killed list in the result shows which processes needed SIGKILL:

// A shell that ignores SIGTERM, with two children that ignore it too
const stubborn = spawn('sh', ['-c', 'trap "" TERM; sleep 60 & sleep 60; wait'])

const result = await endtree(stubborn.pid, { timeout: 1000 })
result.killed  // => [5120, 5121, 5122]  (all three got SIGKILL after 1 second)

Kill only the children, keep the parent

Set includeRoot: false to end every descendant of pid and leave pid itself running. With your own pid, this stops every child process your program started:

await endtree(process.pid, { includeRoot: false })
await descendants(process.pid)  // => []

Send a signal without waiting

Set wait: false to resolve right after the signal is sent. timeout and escalate then do nothing.

await endtree(pid, { wait: false })

endtree expects every signal to make the processes exit. With wait: true (the default), a process that is still running after timeout gets SIGKILL, whatever signal you sent. To send a signal that should not end the processes, such as SIGHUP to reload or SIGUSR1, pass wait: false:

await endtree(pid, { signal: 'SIGHUP', wait: false })

List a process tree without killing it

import { descendants, processTree } from 'endtree'

await descendants(child.pid)
// => [5121, 5122]

await processTree(child.pid)
// => { pid: 5120, ppid: 5110, children: [
//      { pid: 5121, ppid: 5120, children: [] },
//      { pid: 5122, ppid: 5120, children: [] },
//    ] }

Handle errors

Every function returns a promise. Errors reject the promise. They are never thrown synchronously.

import { endtree, EndtreeError } from 'endtree'

try {
  await endtree(pid, { escalate: false, timeout: 1000 })
} catch (e) {
  if (e instanceof EndtreeError && e.code === 'ETIMEDOUT') {
    console.log('still running:', e.pids)  // => still running: [ 5121, 5120 ]
  } else {
    throw e
  }
}

A pid that is not running is not an error:

await endtree(999999)
// => { pid: 999999, signal: 'SIGTERM', signalled: [], killed: [], alreadyExited: [999999] }

API

endtree(pid, options?)

function endtree(pid: number, options?: EndtreeOptions): Promise<EndtreeResult>

Sends a signal to pid and all of its descendants, then waits for them to exit. Also available as the default export.

| Option | Type | Default | Description | |---|---|---|---| | signal | Signal | 'SIGTERM' | Signal name ('SIGINT') or number (9) sent to every process in the tree. | | includeRoot | boolean | true | Also signal pid itself. false ends only its descendants and leaves pid running. | | wait | boolean | true | Wait until every signalled process has exited. false resolves right after signalling, and timeout and escalate then do nothing. | | timeout | number | 5000 | How long to wait, in milliseconds. Infinity waits forever. | | escalate | boolean | true | When timeout passes, send SIGKILL to whatever is still running (and to anything those processes started in the meantime), then wait up to 2 more seconds. false rejects with ETIMEDOUT instead. |

Resolves with an EndtreeResult:

| Property | Type | Description | |---|---|---| | pid | number | The pid you passed. | | signal | Signal | The signal that was sent. | | signalled | number[] | Every pid that got the signal, deepest descendants first. | | killed | number[] | Pids that got SIGKILL after the timeout. Empty if none did. | | alreadyExited | number[] | Pids found in the tree that were gone before they could be signalled. Contains pid itself if it was not running. |

If pid is not running, the promise resolves with signalled: [] and alreadyExited: [pid].

descendants(pid)

function descendants(pid: number): Promise<number[]>

Resolves with the pids of every process descended from pid (not pid itself), parents before children. Resolves with [] if pid is not running.

processTree(pid)

function processTree(pid: number): Promise<ProcessNode | null>

Resolves with pid and its descendants as a nested tree, or null if pid is not running.

interface ProcessNode {
  pid: number
  ppid: number            // parent pid
  children: ProcessNode[]
}

EndtreeError

All functions reject with an EndtreeError (a subclass of Error) when something goes wrong.

| Property | Type | Description | |---|---|---| | code | EndtreeErrorCode | One of the codes below. | | pids | number[] | The pids the error is about (for EPERM and ETIMEDOUT). Empty otherwise. | | result | EndtreeResult \| undefined | What had been done when the error happened (for EPERM and ETIMEDOUT). |

| Code | When | |---|---| | EINVAL | pid is not a positive integer (strings are rejected too), or an option is not valid. | | EPERM | Some processes in the tree belong to another user. All the other processes were still signalled. | | ETIMEDOUT | Processes were still running when the wait ended. | | ELIST | The process list could not be read (ps, PowerShell and wmic all failed). |

Types

import type {
  EndtreeOptions,    // options of endtree()
  EndtreeResult,     // what endtree() resolves with
  ProcessNode,       // a node of processTree()
  Signal,            // SignalName | number
  SignalName,        // 'SIGTERM' | 'SIGKILL' | 'SIGINT' | ...
  EndtreeErrorCode,  // 'EINVAL' | 'EPERM' | 'ETIMEDOUT' | 'ELIST'
} from 'endtree'

CommonJS

The CommonJS export is the endtree function, with the other exports attached to it:

const endtree = require('endtree')

await endtree(pid)
await endtree.descendants(pid)
await endtree.processTree(pid)
endtree.EndtreeError

How it works

Killing a whole tree is a race. While you look for children, processes keep starting new ones. When a parent dies, its children get a new parent (init, or another "reaper" process), so you can no longer find them by starting from the old parent.

Linux, macOS and BSD

  1. Read the process table: /proc/*/stat on Linux, ps -A -o pid= -o ppid= -o stat= -o lstart= elsewhere (or on Linux without /proc).
  2. Send SIGSTOP to every process in the tree, parents first. A stopped process cannot start new ones. Its children keep their parent, because the parent is still alive.
  3. Read the process table again, to find children started between steps 1 and 2. Repeat until nothing new appears (normally one extra round).
  4. Send the signal to every process, deepest first. Then send SIGCONT to all of them so they can act on it (a shell does the same for stopped jobs).
  5. Check until they have exited. Zombies (processes that have exited but were not yet collected by their parent) count as exited. Anything left after timeout goes through steps 1 to 4 again with SIGKILL.

Each process is identified by its pid and its start time. If a pid is reused by an unrelated process in the meantime, that process is never signalled.

If something goes wrong halfway, the frozen processes are always resumed, also when your program exits. Job-control signals (SIGSTOP, SIGTSTP, SIGTTIN, SIGTTOU, SIGCONT) are sent without freezing, because freezing would cancel them. If your own process is part of the tree, it is never frozen and it is signalled last.

Windows

Windows has no signals, and it cannot freeze another process without native code. endtree lists processes with PowerShell (Get-CimInstance Win32_Process, or wmic if that fails), then runs taskkill /T:

  • With signal: 'SIGKILL': taskkill /T /F right away.
  • With any other signal: first taskkill /T without /F, which asks windowed programs to close. Console programs (like node) cannot be closed that way, so if any process refuses, taskkill runs again with /F right away.
  • After a forced kill, any process from the list that is still running (same pid and creation time) is ended directly.

Windows never updates the parent pid of a process. So a "child" that started before its parent is ignored: that parent pid now belongs to an unrelated process.

Comparison

| | tree-kill | endtree | |---|---|---| | API | Callback | Promise | | Waits until the processes have exited | No | Yes | | SIGKILL for processes that ignore SIGTERM | No | Yes, after timeout | | Runs commands without a shell | No (a shell string on Windows, CVE-2019-15599) | Yes, argument arrays only, on every OS | | Linux: how the tree is found | One ps process per process in the tree | Reads /proc, starts nothing | | Children started while the tree is being killed | Can be missed | Found on Linux and macOS (the tree is frozen first) | | Pid reused by another process in the meantime | Not detected | Detected (start times are compared) | | Kill only the children, keep the parent | No | Yes (includeRoot: false) | | List descendants without killing | No | Yes (descendants(), processTree()) | | Clear errors for "not permitted" and "still running" | No | Yes (EPERM, ETIMEDOUT) |

Migrating from tree-kill

Before:

const kill = require('tree-kill')

kill(pid, 'SIGKILL', (err) => { /* ... */ })

After:

const endtree = require('endtree')

await endtree(pid, { signal: 'SIGKILL' })

Two differences:

  • A pid that is not running resolves. It does not fail.
  • The promise settles only after the processes are gone. To get the old "send and forget" behavior, pass wait: false.

Notes and limitations

  • Tested on Linux only. The macOS and Windows code paths are covered by tests that use a simulated system, but they have not been run on a real Mac or Windows machine.
  • Windows has no signals and no freeze step. taskkill /T ends the tree there. The signal option only decides if /F (force) is used right away (SIGKILL) or only after a normal close fails.
  • Any signal is treated as a request to exit. With wait: true, processes still running after timeout get SIGKILL. Use wait: false for signals like SIGHUP or SIGUSR1.
  • Processes of other users cannot be signalled without permission. endtree signals all the others and then rejects with EPERM.
  • SIGKILL gets 2 more seconds. If a process is still running after that, the promise rejects with ETIMEDOUT.

Requirements

  • Node.js 20 or newer. Also works in Bun.
  • Linux, macOS (and BSD) or Windows.
  • ESM and CommonJS.
  • TypeScript types included.
  • Zero dependencies.

License

MIT