janitor
janitor runs a command in its own process group and tears that group down when
its owner goes away.
It is meant for development stacks where one layer can die without running a
clean shutdown: shells, Claude Code sessions, zmx, Tilt, serve_cmd, local
chains, indexers, dev servers, and similar long-running commands.
Why
Process trees often outlive the thing that started them. If the parent shell,
terminal session, editor agent, or Tilt process exits unexpectedly, child
services can be reparented to launchd, systemd, or another subreaper and
keep ports, files, databases, and CPU alive.
janitor makes that ownership explicit:
- Capture the original parent PID.
- Spawn the command as a new process-group leader.
- Watch the parent, child, signals, and optional worktree path.
- On any death trigger, snapshot the direct child's live PPID descendants.
- Send
SIGTERM to the child process group and individually to verified
descendants that escaped it.
- Wait for the grace window, then send
SIGKILL if anything remains.
Install
The installer defaults to the latest stable GitHub Release and selects the
archive for the current platform.
curl -fsSL https://raw.githubusercontent.com/alleneubank/janitor/main/install.sh | sh
By default this installs to ~/.local/bin/janitor. Override with PREFIX:
curl -fsSL https://raw.githubusercontent.com/alleneubank/janitor/main/install.sh | PREFIX=/usr/local sh
Install a specific release tag:
curl -fsSL https://raw.githubusercontent.com/alleneubank/janitor/main/install.sh | JANITOR_VERSION=v0.1.0 sh
Source installation remains available for unsupported platforms and maintainer
testing. It requires Zig 0.15.2 or newer:
JANITOR_INSTALL_FROM_SOURCE=1 ./install.sh
Usage
janitor [--watch-path PATH] [--watch-pid PID] [--grace-ms MS] [--poll-ms MS] [--pgroup-only] -- CMD [ARGS...]
janitor version | --version | -V
janitor version (or --version / -V) prints janitor <version> (<sha>),
where <sha> is the short git commit the binary was built from (unknown when
built outside a git checkout), so a deployed binary can be matched to its source.
Examples:
janitor --watch-path "$PWD" -- yarn localnet:up
janitor --watch-path "$PWD" -- bun run src/index.ts daemon
janitor --grace-ms 500 -- tilt up
--watch-path is useful for worktree-based development. If the worktree is
deleted or moved, janitor treats that as a teardown trigger.
--watch-pid watches an arbitrary process by PID. When that process exits,
janitor treats it as a teardown trigger. This is useful when the supervised
command is reparented away from janitor, so watching the owning process
directly is more reliable than relying on janitor's immediate parent.
--poll-ms is accepted for compatibility with earlier development builds. On
supported platforms, the active watcher is event-driven and does not use
periodic idle polling.
By default, teardown snapshots the direct child's live PPID-linked descendants
before sending SIGTERM. Janitor keeps its original process-group fast path,
and only individually signals an escaped descendant after platform identity
verification proves it is the captured process. Use --pgroup-only to opt out
and retain the historical behavior that drains only the original child process
group.
Platform Behavior
- macOS and BSD use
kqueue for process, signal, vnode, and timeout waits.
- Linux uses
epoll over pidfd, signalfd, and inotify.
- Process watches, including the original parent, child, and any
--watch-pid
target, use EVFILT_PROC / NOTE_EXIT on macOS/BSD and pidfd on Linux.
- Windows is not supported.
Snapshot descendant draining is supported on Linux and macOS. Other BSD
platforms retain the original process-group-only teardown: Janitor reports that
the descendant snapshot is unavailable and never guesses individual targets.
Those BSD backends cannot observe a child exit without reaping its group
leader. Once that happens Janitor stops issuing numeric process-group signals,
because a recycled PGID could otherwise target an unrelated group; this
deliberately favors signal safety over any remaining group cleanup breadth.
The child command is started in a new process group. Janitor signals only that
group wholesale; escaped descendants are individually addressed through their
captured identity. Unsupported descendant-discovery backends, including BSD
platforms other than macOS, diagnose the limitation and retain group-only
teardown rather than guessing at targets.
Claude Code plugin
The bundled Claude Code plugin wraps the Bash tool so processes a session
starts are drained by janitor when the session ends, including crashes,
/clear, and closed terminals.