claude-code - 💡(How to fix) Fix Monitor tool and run_in_background Bash exit 1 spuriously when polling `gh` in a loop [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#54983Fetched 2026-05-01 05:49:19
View on GitHub
Comments
0
Participants
1
Timeline
5
Reactions
0
Participants
Timeline (top)
labeled ×5

Error Message

Error Messages / Logs

Fix Action

Workaround

Wrapping in explicit bash -c '...' resolves the issue:

bash -c '
while true; do
  out=$(gh run view 12345678 --json status,conclusion 2>/dev/null)
  status=$(echo "$out" | jq -r .status 2>/dev/null)
  echo "status=$status"
  if [ "$status" = "completed" ]; then break; fi
  sleep 30
done
'

Code Example

while true; do
  result=$(gh run view 12345678 --json status,conclusion 2>/dev/null)
  status=$(echo "$result" | jq -r .status)
  echo "status=$status"
  [ "$status" = "completed" ] && break
  sleep 30
done

---

Monitor "..." script failed (exit 1)
Background command "..." failed with exit code 1

---

bash -c '
while true; do
  out=$(gh run view 12345678 --json status,conclusion 2>/dev/null)
  status=$(echo "$out" | jq -r .status 2>/dev/null)
  echo "status=$status"
  if [ "$status" = "completed" ]; then break; fi
  sleep 30
done
'
RAW_BUFFERClick to expand / collapse

Preflight Checklist

  • I have searched existing issues and this hasn't been reported
  • This is a single bug

What's Wrong?

When using the Monitor tool or Bash with run_in_background: true to poll gh run view in a loop, the script exits with code 1 even though all individual commands succeed when run interactively.

What Should Happen?

The loop should run until the condition is met and exit 0.

Steps to Reproduce

  1. Ask Claude to monitor a GitHub Actions run using the Monitor tool or run_in_background
  2. Claude generates a poll loop like:
while true; do
  result=$(gh run view 12345678 --json status,conclusion 2>/dev/null)
  status=$(echo "$result" | jq -r .status)
  echo "status=$status"
  [ "$status" = "completed" ] && break
  sleep 30
done
  1. Script exits immediately with exit code 1

Verification that individual commands work fine when run interactively:

  • gh run view 12345678 --json status,conclusion → exit 0
  • echo '...' | jq -r .status → exit 0

Error Messages / Logs

Monitor "..." script failed (exit 1)
Background command "..." failed with exit code 1

Workaround

Wrapping in explicit bash -c '...' resolves the issue:

bash -c '
while true; do
  out=$(gh run view 12345678 --json status,conclusion 2>/dev/null)
  status=$(echo "$out" | jq -r .status 2>/dev/null)
  echo "status=$status"
  if [ "$status" = "completed" ]; then break; fi
  sleep 30
done
'

Suspected Causes

  1. Shell env difference between interactive session and Monitor/background task subprocess
  2. Emoji in job name (e.g. 📦 Build) from gh run view --json jobs output causing multibyte character parsing issues in the subprocess env

Environment

  • Claude model: Claude Sonnet 4.6
  • Claude Code version: latest
  • API platform: Anthropic API
  • OS: macOS
  • Shell: zsh

extent analysis

TL;DR

The issue can be resolved by wrapping the script in bash -c '...' to ensure consistent shell environment and handling of multibyte characters.

Guidance

  • Verify that the issue is indeed caused by the shell environment difference by comparing the output of env command in both interactive and Monitor/background task subprocess sessions.
  • Check for any emoji or multibyte characters in the job name or output of gh run view --json jobs that could be causing parsing issues.
  • Consider adding error handling to the script to catch and log any errors that may be occurring, such as set -e to exit on error or trap to catch signals.
  • Test the workaround with different job names and outputs to ensure it is a robust solution.

Example

bash -c '
  set -e
  while true; do
    out=$(gh run view 12345678 --json status,conclusion 2>/dev/null)
    status=$(echo "$out" | jq -r .status 2>/dev/null)
    echo "status=$status"
    if [ "$status" = "completed" ]; then break; fi
    sleep 30
  done
'

Notes

The issue may be specific to the Claude model or API platform being used, and further investigation may be needed to determine the root cause. Additionally, the workaround may have unintended consequences, such as changing the shell environment or behavior of the script.

Recommendation

Apply the workaround by wrapping the script in bash -c '...' to ensure consistent shell environment and handling of multibyte characters, as it has been shown to resolve the issue in the provided example.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix Monitor tool and run_in_background Bash exit 1 spuriously when polling `gh` in a loop [1 participants]