|
| 1 | +--- |
| 2 | +name: self-terminate |
| 3 | +description: Gracefully terminate the Claude Code session by sending SIGINT to the Claude process |
| 4 | +--- |
| 5 | + |
| 6 | +# Self-Terminate Skill |
| 7 | + |
| 8 | +This skill enables Claude to gracefully terminate its own session by sending a SIGINT signal to its process. |
| 9 | + |
| 10 | +## When to Use This Skill |
| 11 | + |
| 12 | +- When the user explicitly asks Claude to exit or terminate |
| 13 | +- When Claude needs to restart with a fresh session |
| 14 | +- When testing process management or signal handling |
| 15 | + |
| 16 | +## How It Works |
| 17 | + |
| 18 | +Claude runs as a process that spawns shell subprocesses for Bash commands. The parent PID (`$PPID`) of any spawned shell is the Claude process itself. |
| 19 | + |
| 20 | +Sending `SIGINT` (signal 2) to the Claude process triggers a graceful shutdown, similar to pressing `Ctrl+C`. |
| 21 | + |
| 22 | +## Quick Method: Use the Script |
| 23 | + |
| 24 | +The easiest way is to execute the provided script: |
| 25 | + |
| 26 | +```bash |
| 27 | +/path/to/plugins/self-terminate/bin/self-terminate.sh |
| 28 | +``` |
| 29 | + |
| 30 | +Or if the plugin is installed: |
| 31 | + |
| 32 | +```bash |
| 33 | +~/.claude/plugins/self-terminate/bin/self-terminate.sh |
| 34 | +``` |
| 35 | + |
| 36 | +## Manual Method |
| 37 | + |
| 38 | +If the script is unavailable, Claude can terminate itself manually: |
| 39 | + |
| 40 | +### Step 1: Identify the Claude Process |
| 41 | + |
| 42 | +```bash |
| 43 | +echo "Shell PID: $$" |
| 44 | +echo "Claude PID (parent): $PPID" |
| 45 | +ps -o pid,ppid,comm -p $$ -p $PPID |
| 46 | +``` |
| 47 | + |
| 48 | +### Step 2: Verify It's Claude |
| 49 | + |
| 50 | +```bash |
| 51 | +ps -o comm= -p $PPID |
| 52 | +``` |
| 53 | + |
| 54 | +This should output `claude` or similar. |
| 55 | + |
| 56 | +### Step 3: Send SIGINT |
| 57 | + |
| 58 | +```bash |
| 59 | +kill -INT $PPID |
| 60 | +``` |
| 61 | + |
| 62 | +## Process Tree Context |
| 63 | + |
| 64 | +A typical Claude Code process tree looks like: |
| 65 | + |
| 66 | +``` |
| 67 | +iTerm/Terminal |
| 68 | +└── shell (user's interactive shell) |
| 69 | + └── claude (PID: XXXXX) ← Target this |
| 70 | + └── /bin/zsh (spawned for Bash commands) |
| 71 | + └── (your command) |
| 72 | +``` |
| 73 | + |
| 74 | +## Safety Notes |
| 75 | + |
| 76 | +- **SIGINT** causes graceful termination - Claude can clean up |
| 77 | +- **SIGTERM** also works for graceful shutdown |
| 78 | +- **SIGKILL** (-9) should be avoided - no cleanup opportunity |
| 79 | +- The script verifies the parent is actually Claude before sending the signal |
| 80 | + |
| 81 | +## What Happens After |
| 82 | + |
| 83 | +After termination: |
| 84 | +1. The Claude session ends immediately |
| 85 | +2. Any in-progress work is interrupted |
| 86 | +3. The user returns to their shell |
| 87 | +4. A new session can be started with `claude` |
| 88 | + |
| 89 | +## Troubleshooting |
| 90 | + |
| 91 | +**Script says parent is not Claude**: You may be running in a nested shell or different environment. Check `pstree -p $$` to see the full process tree. |
| 92 | + |
| 93 | +**Signal ignored**: Some environments may mask signals. Try `kill -TERM $PPID` as an alternative. |
0 commit comments