一键导入
claude-orphan-cleanup
Find and terminate orphaned Claude Code CLI processes that consume CPU resources. Works on macOS and Linux systems.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Find and terminate orphaned Claude Code CLI processes that consume CPU resources. Works on macOS and Linux systems.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Multi-agent review of the current diff (or a PR/MR number, branch, or file target) for correctness bugs plus reuse, simplification, efficiency, altitude, and conventions cleanups, at a selectable effort level. Low/medium surface fewer, high-confidence findings; high/xhigh/max widen coverage (language pitfalls, security, concurrency, wrapper correctness) and may include uncertain findings. Use when the user asks to review a diff, branch, MR, or PR, or to hunt for bugs before committing. Pass --comment to post findings as inline PR/MR comments, or --fix to apply the surviving findings to the working tree after the review.
Produce a complete, navigable project-state documentation set for any software project (mobile, web, desktop, backend, or monorepo) — architecture, data, flows, API, tests, security, state/sync, performance, observability, platform parity, config, and health. Use when the user wants a full-codebase audit, architecture documentation, an onboarding "helicopter view", a regenerable project snapshot to diff over time, or a grounded top-10 risks / action-items report. Orchestrates parallel "scout" agents in two waves plus a synthesis pass, emitting markdown plus self-contained interactive HTML per section.
Review the changed code for reuse, simplification, efficiency, and altitude cleanups, then apply the fixes. Quality only — it does not hunt for bugs; use /code-review for that.
Plan, prioritize, and sequence implementation work so every iteration ends with a working version the end-user can already benefit from. Applies a "progressive JPEG" principle — start with a thin core scaffold that already delivers real value, then add features in order of user-visible value. Bugs in already-shipped functionality come first; engineering for hypothetical future steps is rejected. Triggers on "what's next", "plan the next steps", "prioritize features", "order of implementation", "MVP plan", "roadmap", "what should ship first", or any other request that orders multi-step work.
Scaffold or update a project to match the standard CLAUDE.md structure — root CLAUDE.md, docs/ tree (architecture, source-map, testing trio, workflow, gotchas, deploy, api), docs/ephemeral/ working area, and .claude/ agent + skill stubs. Use when starting a new repo, adopting the standard layout in an existing repo, or repairing a partial scaffold. Skips existing files unless --force.
Connect to a remote tmux session over SSH using iTerm2's native tmux control mode (-CC), creating the session if missing and detaching any stale client. Works on macOS with iTerm2.
| name | claude-orphan-cleanup |
| description | Find and terminate orphaned Claude Code CLI processes that consume CPU resources. Works on macOS and Linux systems. |
| author | Greg Z. |
| author_email | info@extractum.io |
| author_url | https://www.linkedin.com/in/gregzem/ |
When using Claude Code CLI (claude command), the tool spawns bash subprocesses for various operations:
If the main Claude process crashes, is interrupted (Ctrl+C), or terminates unexpectedly, these child processes can become orphaned — adopted by the init process (PID 1 on Linux, launchd on macOS) and left running indefinitely.
bash processes consuming 100% CPUclaude, shell-snapshot, or claude_agent_sdk in their command lineWhen this skill is invoked, follow these steps to identify and clean up orphaned Claude Code processes:
Run the cleanup tool in dry-run mode (default) to list orphaned processes:
claude-cleanup
The tool searches for processes matching these criteria:
shell-snapshot — shell environment capture mechanismclaude_agent_sdk — internal SDK components_bundled/claude — bundled binariesclaude-[a-f0-9]+-cwd — temp files with hex IDsSNAPSHOT_FILE=*/.claude/ — snapshot paths (any OS)claude --ripgrep — bundled ripgrep wrapper/.claude/shell-snapshots — snapshot directory referencesThe output displays:
Example output:
=== Claude Code Orphan Process Finder ===
Found 3 orphaned Claude Code process(es):
PID CPU% ELAPSED COMMAND (truncated)
--------------------------------------------------------------------------------
11409 100.0% 16:23:34 /bin/bash -c -l { shopt -u extglob || setopt NO_EXTENDED_GLO...
96647 99.1% 17:16:44 /bin/bash -c -l SNAPSHOT_FILE=$HOME/.claude/shell-snap...
98091 99.2% 01-11:38:47 /bin/bash -c -l SNAPSHOT_FILE=$HOME/.claude/shell-snap...
Run with --kill to terminate these processes
Run with --force to kill without confirmation
Once you've confirmed the processes are orphaned Claude processes, kill them:
With confirmation prompt:
claude-cleanup --kill
Without confirmation (for scripts/automation):
claude-cleanup --force
Run the tool again to confirm all orphans have been terminated:
claude-cleanup
Expected output:
=== Claude Code Orphan Process Finder ===
✓ No orphaned Claude Code processes found
Scenario: You've been using Claude Code extensively and notice high CPU usage even when Claude is idle.
Execution:
# Check for orphans
claude-cleanup
# Output shows 5 stuck processes at 100% CPU
# Kill them with confirmation
claude-cleanup --kill
# Type 'y' when prompted
# Verify cleanup
claude-cleanup
# Output: ✓ No orphaned Claude Code processes found
Result:
Scenario: You want to automatically clean up orphaned processes every hour.
Setup:
# Edit crontab
crontab -e
# Add hourly cleanup job
0 * * * * $HOME/bin/claude-cleanup --force >> /tmp/claude-cleanup.log 2>&1
Benefits:
Scenario: You need to clean up orphans but don't have the script installed.
Execution:
# Find and display orphaned Claude processes
ps -eo pid,ppid,pcpu,args | awk '$2==1' | grep -E 'claude|shell-snapshot|SNAPSHOT_FILE'
# Kill all matching processes
ps -eo pid,ppid,args | awk '$2==1' | grep -E 'claude|shell-snapshot' | awk '{print $1}' | xargs kill -9
Note: The script version is safer as it uses more specific patterns and provides confirmation.
Scenario: You want to understand what a specific orphaned process was doing before killing it.
Execution:
# Get full command line for a specific PID
ps -p 11409 -o pid,ppid,lstart,args
# Check what files/resources the process has open
lsof -p 11409
# Check the working directory
lsof -p 11409 | grep cwd
Output interpretation:
shell-snapshot processes were capturing shell environmentcurl commands were MCP tool network requestsScenario: You prefer macOS-native scheduling over cron.
Setup:
cat > ~/Library/LaunchAgents/com.user.claude-cleanup.plist << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.user.claude-cleanup</string>
<key>ProgramArguments</key>
<array>
<string>/Users/YOUR_USERNAME/bin/claude-cleanup</string>
<string>--force</string>
</array>
<key>StartInterval</key>
<integer>3600</integer>
<key>StandardOutPath</key>
<string>/tmp/claude-cleanup.log</string>
<key>StandardErrorPath</key>
<string>/tmp/claude-cleanup.log</string>
</dict>
</plist>
EOF
launchctl load ~/Library/LaunchAgents/com.user.claude-cleanup.plist
launchctl list | grep claude-cleanup
Benefits:
| Command | Description |
|---|---|
claude-cleanup | List orphaned processes (dry run) |
claude-cleanup --kill or -k | Kill processes with confirmation |
claude-cleanup --force or -f | Kill processes without confirmation |
claude-cleanup --help or -h | Show help message |
Below is the complete bash script that implements this skill:
#!/bin/bash
#
# claude-cleanup - Find and kill orphaned Claude Code processes
#
# Orphaned Claude processes typically have:
# - PPID=1 (adopted by init/launchd after parent died)
# - Command patterns related to claude_agent_sdk, shell-snapshots, or MCP tools
# - Often consuming 100% CPU in stuck state
#
# Usage:
# claude-cleanup # List orphaned Claude processes (dry run)
# claude-cleanup --kill # Kill all found orphaned processes
# claude-cleanup --force # Kill without confirmation
#
set -euo pipefail
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
KILL_MODE=false
FORCE_MODE=false
while [[ $# -gt 0 ]]; do
case $1 in
--kill|-k)
KILL_MODE=true
shift
;;
--force|-f)
KILL_MODE=true
FORCE_MODE=true
shift
;;
--help|-h)
echo "Usage: claude-cleanup [OPTIONS]"
echo ""
echo "Find and kill orphaned Claude Code processes"
echo ""
echo "Options:"
echo " --kill, -k Kill found orphaned processes (with confirmation)"
echo " --force, -f Kill without confirmation"
echo " --help, -h Show this help message"
exit 0
;;
*)
echo "Unknown option: $1"
exit 1
;;
esac
done
# Patterns that identify Claude Code processes (cross-platform)
CLAUDE_PATTERNS=(
# Shell environment snapshot mechanism
"shell-snapshot"
# Claude Agent SDK internals
"claude_agent_sdk"
"_bundled/claude"
# Temp files for tracking CWD (hex ID pattern)
"claude-[a-f0-9]+-cwd"
# Snapshot files in .claude directory (any OS home path)
"SNAPSHOT_FILE=.*/\\.claude/"
# Claude's bundled ripgrep wrapper
"claude --ripgrep"
# Generic .claude config directory in command args
"/\\.claude/shell-snapshots"
)
# Build grep pattern
GREP_PATTERN=$(IFS='|'; echo "${CLAUDE_PATTERNS[*]}")
echo -e "${BLUE}=== Claude Code Orphan Process Finder ===${NC}"
echo ""
# Find orphaned processes (PPID=1) matching Claude patterns
# Using ps to get all processes, then filtering
ORPHANS=$(ps -eo pid,ppid,pcpu,etime,args 2>/dev/null | \
awk '$2 == 1' | \
grep -E "$GREP_PATTERN" 2>/dev/null || true)
if [[ -z "$ORPHANS" ]]; then
echo -e "${GREEN}✓ No orphaned Claude Code processes found${NC}"
exit 0
fi
# Count and display
COUNT=$(echo "$ORPHANS" | wc -l | tr -d ' ')
echo -e "${YELLOW}Found $COUNT orphaned Claude Code process(es):${NC}"
echo ""
# Header
printf "${BLUE}%-8s %-8s %-12s %s${NC}\n" "PID" "CPU%" "ELAPSED" "COMMAND (truncated)"
echo "--------------------------------------------------------------------------------"
# Display each orphan
echo "$ORPHANS" | while read -r line; do
PID=$(echo "$line" | awk '{print $1}')
CPU=$(echo "$line" | awk '{print $3}')
ELAPSED=$(echo "$line" | awk '{print $4}')
CMD=$(echo "$line" | awk '{$1=$2=$3=$4=""; print substr($0, 5, 60)}')
# Color based on CPU usage
if (( $(echo "$CPU > 50" | bc -l 2>/dev/null || echo 0) )); then
printf "${RED}%-8s %-8s %-12s %s...${NC}\n" "$PID" "$CPU%" "$ELAPSED" "$CMD"
else
printf "%-8s %-8s %-12s %s...\n" "$PID" "$CPU%" "$ELAPSED" "$CMD"
fi
done
echo ""
if [[ "$KILL_MODE" == true ]]; then
PIDS=$(echo "$ORPHANS" | awk '{print $1}' | tr '\n' ' ')
if [[ "$FORCE_MODE" == false ]]; then
echo -e "${YELLOW}Kill these processes? [y/N]${NC} "
read -r CONFIRM
if [[ ! "$CONFIRM" =~ ^[Yy]$ ]]; then
echo "Aborted."
exit 0
fi
fi
echo -e "${RED}Killing processes: $PIDS${NC}"
for pid in $PIDS; do
if kill -9 "$pid" 2>/dev/null; then
echo -e "${GREEN} ✓ Killed PID $pid${NC}"
else
echo -e "${YELLOW} ⚠ Could not kill PID $pid (may have already exited)${NC}"
fi
done
echo ""
echo -e "${GREEN}Done!${NC}"
else
echo -e "${BLUE}Run with --kill to terminate these processes${NC}"
echo -e "${BLUE}Run with --force to kill without confirmation${NC}"
fi
Check for non-orphaned Claude processes:
ps aux | grep -E 'claude|node.*claude' | grep -v grep
These may be active Claude sessions. Close them normally or kill if unresponsive.
Ensure you have permission to kill the process (same user or root):
sudo claude-cleanup --force
Ensure the script is in your PATH:
# Check if ~/bin is in PATH
echo $PATH | grep -q "$HOME/bin" && echo "OK" || echo "Add ~/bin to PATH"
# Or run with full path
$HOME/bin/claude-cleanup