iTranslated by AI
Fixing Claude Code Hangs in Cursor Agent via a Wrapper Script
For those who just want the conclusion
- Executing
claude -pvia the Cursor Agent from Cursor's chat hangs indefinitely. - It works when using a pipe (
echo ... | claude) → behavior changes based on whether stdout/stderr is a TTY. - Resolved by using a wrapper script that uses
nohup+ output redirection to send process output to a file. - Code for the wrapper script (same as the sample in the article).
Background: What I wanted to achieve
Cursor Agent and Claude Code operate with different models and contexts. If so, I should be able to create a quasi-multi-agent setup by chatting with the Agent in a Cursor chat thread and having it run claude -p with instructions like "Ask Claude Code about this."
For example:
- Let Cursor Agent handle code implementation, while asking Claude Code for reviews or second opinions.
- Maintain context on the Claude Code side using
-c(session continuation) and assign tasks incrementally.
It works if you trigger claude -p manually or by opening a terminal pane within Cursor. However, that doesn't allow for seamless integration within the conversation flow with the Cursor Agent. I wanted the Agent to execute it via the terminal to provide second opinions or reviews.
When I attempted this verification, I ran into the problem that I couldn't receive a response from claude -p when using the Agent.
Environment
| Item | Version |
|---|---|
| macOS | 15.7.3 (24G419) |
| Cursor | 2.4.28 (arm64) |
| Claude CLI | 2.1.31 (Claude Code) |
| Node.js | 18.17.1 |
What happens
When you have the Cursor Agent execute claude -p in the terminal, it hangs without returning any output. I have confirmed the same behavior when executing directly from the cursor-agent command (Cursor CLI).
# When executed in the terminal by the Agent from Cursor chat
$ claude -p "Hello, respond with OK"
# → No output even after waiting for over 60 seconds. Must be killed.
# The same happens when executed directly from the cursor-agent command
$ cursor-agent claude -p "Hello, respond with OK"
# → Hangs similarly
On the other hand, it works normally when passed from standard input via a pipe.
$ echo "Hello, respond with OK" | claude --output-format text
OK
# → Response in about 5 seconds
Executing -p directly from an external terminal (like WezTerm) also works without any issues.
Why does it work with a pipe?
It is likely that the CLI's input detection logic differs between the -p flag and a pipe.
-
Pipe: Detects that stdin is a pipe via
isatty(0)etc. → Reads purely as a stream. -
-pflag: stdin remains a TTY, switches to non-interactive mode via the flag → TTY control setup is triggered.
Since the terminal operated by the Cursor Agent (and the cursor-agent command) runs in a PTY environment different from the standard one, it is presumed that a conflict in TTY control is occurring in the latter path.
Solution: nohup wrapper
Redirect the process stdout/stderr to a file to bypass TTY detection on the CLI side.
Script
#!/bin/bash
# claude-safe - A wrapper to safely execute Claude CLI via Cursor Agent
#
# Usage:
# claude-safe -p "prompt" --output-format text
# claude-safe -c -p "session continuation"
# DEBUG=1 claude-safe -p "with debug output"
set -euo pipefail
# Debug mode
DEBUG=${DEBUG:-0}
debug_log() {
if [ "$DEBUG" = "1" ]; then
echo "[DEBUG] $*" >&2
fi
}
# Output files
OUTPUT_DIR="${HOME}/.claude_wrapper"
mkdir -p "$OUTPUT_DIR"
OUTPUT_FILE="${OUTPUT_DIR}/output_$$.txt"
ERROR_FILE="${OUTPUT_DIR}/error_$$.txt"
# Cleanup
cleanup() {
debug_log "Cleaning up..."
rm -f "$OUTPUT_FILE" "$ERROR_FILE"
}
trap cleanup EXIT
debug_log "Running: claude $*"
debug_log "Output file: $OUTPUT_FILE"
# Execute Claude (Process isolation - macOS compatible)
# Isolate process with nohup and background execution
nohup claude "$@" > "$OUTPUT_FILE" 2> "$ERROR_FILE" &
CLAUDE_PID=$!
# Wait for process completion (preserving exit code while preventing immediate exit via set -e)
wait $CLAUDE_PID && EXIT_CODE=0 || EXIT_CODE=$?
debug_log "Exit code: $EXIT_CODE"
# Output results
cat "$OUTPUT_FILE"
if [ -s "$ERROR_FILE" ]; then
cat "$ERROR_FILE" >&2
fi
exit $EXIT_CODE
Setup
# Place the script (anywhere in your PATH)
cp claude-safe ~/.local/bin/claude-safe
chmod +x ~/.local/bin/claude-safe
Why this works
Cursor Agent terminal (proprietary PTY environment)
└─ claude-safe (wrapper)
└─ nohup claude ... > file 2> file & (stdout/stderr to file → non-TTY detection)
└─ API call → Write to temporary file
└─ wait → cat → Display to standard output
The key here is output redirection using > "$OUTPUT_FILE" 2> "$ERROR_FILE". By pointing the Claude CLI's stdout/stderr to a file instead of a TTY, the CLI's TTY detection (like isatty()) determines it is "non-TTY," and the TTY control setup is skipped. The logic is the same as why it works with pipes.
nohup itself is a command to ignore the SIGHUP signal, and its primary purpose is not TTY detachment. Essentially, the "non-TTY" state induced by redirection is what makes it work. Strictly speaking, it might work with just redirection + background execution without nohup, but I have leaned towards the safe side by including HUP resistance.
Note that I have not verified exactly which file descriptors (stdin/stdout/stderr) are being checked for TTY status within the CLI. This workaround is a practical solution that proved effective in this environment.
Verification of Results
Here are the comparison results based on the terminal execution within Cursor threads (verified through multiple runs to ensure reproducibility).
❌ claude -p (Hangs)
$ claude -p "Hello! This is a connectivity test. Please respond with OK."
# → No output even after 90 seconds. Force quit with kill.
✅ Via claude-safe (Normal operation)
$ claude-safe -p "Hello! This is a connectivity test. Please respond with OK."
OK - Claude Opus 4.5
# → Responded in about 6 seconds, exit code 0
Comparison
| Execution Method | Execution Environment | Result | Time Required |
|---|---|---|---|
claude -p "..." |
Via Cursor Agent | ❌ Hang | Over 60s (No timeout) |
| `echo "..." | claude --output-format text` | Via Cursor Agent | ✅ Works |
claude-safe -p "..." |
Via Cursor Agent | ✅ Works | Approx 6s |
claude -p "..." |
External Terminal (WezTerm) | ✅ Works | Approx 5s |
Although it works via pipes, the wrapper method is more practical as it allows you to use the full suite of -p options (such as session continuation with -c, --allowedTools, etc.).
Summary
- The hanging of
claude -pvia Cursor Agent is likely caused by a conflict in PTY control. - This can be resolved by using a wrapper that redirects output to a file, bypassing the CLI's TTY detection.
- The essence of the workaround is not
nohup, but the "non-TTY" state achieved through redirection. - The fact that it works via pipes supports the hypothesis that TTY control is the cause.
I hope this proves useful to anyone trying to do the same thing.
Discussion