Background bash commands become orphan processes after session close or task interruption

Resolved 💬 3 comments Opened Feb 23, 2026 by Disentinel Closed Feb 27, 2026

Description

When Claude Code launches bash commands with run_in_background: true, the spawned processes (node, zsh, etc.) become orphan processes if:

  1. The user interrupts the parent task (Ctrl+C on a Task agent)
  2. The Claude Code session is closed while background commands are still running

These orphaned processes persist indefinitely, consuming RAM and CPU. There is no mechanism to track or clean them up in subsequent sessions.

Reproduction

  1. Start a Claude Code session
  2. Have Claude run a long-running background command (e.g., pnpm build && node --test ... with run_in_background: true)
  3. Interrupt the parent task before the background command completes
  4. Close the Claude Code session
  5. Run ps aux | grep node — the background process is still alive

In my case, I found 3 orphaned node processes from previous sessions (started on Saturday, still running on Sunday):

vadimr  58571  0.0  0.1  node -e <inline debug script>     # from Saturday 10:xx
vadimr  23486  0.0  0.1  node /tmp/test-new-expr.mjs       # from Saturday 09:xx  
vadimr  23130  0.0  0.1  node /tmp/test-new-expr.mjs       # from Saturday 09:xx

Impact

  • Each orphan process holds ~20MB+ RAM
  • With multiple Claude Code sessions (worktree workflow), orphans accumulate fast
  • On a 16GB machine running 4-6 Claude Code instances, this contributes to memory pressure and swap thrashing
  • No visibility in Claude Code UI — the status bar doesn't show these processes in the next session

Expected behavior

  • Background processes should be killed when the parent task is interrupted
  • On session close, all background processes spawned by that session should be cleaned up
  • Alternatively: on session start, Claude Code could check for and clean up orphaned processes from previous sessions (e.g., via a PID file)

Environment

  • macOS 13.5 (Darwin 22.6.0)
  • Claude Code (latest as of 2025-02-23)
  • Node.js v22.22.0

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗