Skip to content

Commit 9caa408

Browse files
nsheapsclaude
andcommitted
fix(self-terminate): traverse process tree to find Claude
The script now walks up the process tree instead of only checking the immediate parent. This handles cases where there are intermediate shells between the spawned command and the Claude process. Co-Authored-By: Claude Code (~/.ai.worktrees/subagent-via-tmux) <noreply@anthropic.com>
1 parent 20d06eb commit 9caa408

1 file changed

Lines changed: 40 additions & 7 deletions

File tree

plugins/self-terminate/bin/self-terminate.sh

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,49 @@
44

55
set -euo pipefail
66

7-
# Get the parent PID (Claude process)
8-
CLAUDE_PID="$PPID"
7+
# Traverse up the process tree to find Claude
8+
find_claude_pid() {
9+
local pid="$PPID"
10+
local max_depth=10
11+
local depth=0
912

10-
# Verify it's actually a claude process
11-
PROCESS_NAME=$(ps -o comm= -p "$CLAUDE_PID" 2>/dev/null || echo "unknown")
13+
while [[ $depth -lt $max_depth ]]; do
14+
local process_name
15+
process_name=$(ps -o comm= -p "$pid" 2>/dev/null || echo "")
1216

13-
if [[ "$PROCESS_NAME" != *"claude"* ]]; then
14-
echo "Error: Parent process ($CLAUDE_PID) is not Claude (found: $PROCESS_NAME)" >&2
17+
if [[ -z "$process_name" ]]; then
18+
echo "Error: Could not find Claude in process tree (reached PID $pid)" >&2
19+
return 1
20+
fi
21+
22+
if [[ "$process_name" == *"claude"* ]]; then
23+
echo "$pid"
24+
return 0
25+
fi
26+
27+
# Get parent of current pid
28+
local parent_pid
29+
parent_pid=$(ps -o ppid= -p "$pid" 2>/dev/null | tr -d ' ')
30+
31+
if [[ -z "$parent_pid" || "$parent_pid" == "0" || "$parent_pid" == "1" ]]; then
32+
echo "Error: Reached init without finding Claude" >&2
33+
return 1
34+
fi
35+
36+
pid="$parent_pid"
37+
((depth++))
38+
done
39+
40+
echo "Error: Max depth reached without finding Claude" >&2
41+
return 1
42+
}
43+
44+
CLAUDE_PID=$(find_claude_pid)
45+
46+
if [[ -z "$CLAUDE_PID" ]]; then
1547
exit 1
1648
fi
1749

18-
echo "Sending SIGINT to Claude process (PID: $CLAUDE_PID)..."
50+
echo "Found Claude process (PID: $CLAUDE_PID)"
51+
echo "Sending SIGINT..."
1952
kill -INT "$CLAUDE_PID"

0 commit comments

Comments
 (0)