Hooks fail with posix_spawn '/bin/sh' ENOENT without sandbox enabled
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
All 4 Stop hooks fail with ENOENT: no such file or directory, posix_spawn '/bin/sh', without the sandbox feature enabled (no sandbox key in settings.json, no bubblewrap processes associated with Claude Code).
● Ran 4 stop hooks
⎿ command: "/home/user/agentic/claude-hooks/emit-event.py"
⎿ command: "$HOME/agentic/claude-hooks/chat-update.py"
⎿ command: "$HOME/agentic/claude-hooks/hooks/notify-stop.sh"
⎿ command: "$HOME/agentic/claude-hooks/log-event.py"
⎿ Stop hook error: Failed with non-blocking status code: Error occurred while
executing hook command: ENOENT: no such file or directory, posix_spawn '/bin/sh'
⎿ (repeated for all 4 hooks)
Investigation
/bin/shexists and is executable (lrwxrwxrwx /bin/sh -> bash)- Running the hook commands manually via
/bin/sh -c '"$HOME/agentic/claude-hooks/log-event.py"'works fine - Sandbox is NOT enabled — no
sandboxkey in settings.json, nobwrapprocesses associated with Claude Code, process is in the init namespaces - The hook scripts themselves have correct shebangs and execute successfully when invoked manually
- The error is transient — hooks in the same session executed successfully on subsequent turns
Hypothesis: CWD-related posix_spawn failure
A comment on #20211 by @danyelf identified that posix_spawn returns ENOENT when the current working directory doesn't exist at spawn time, but the error message misleadingly attributes it to the binary path (/bin/sh). This is a well-known POSIX behavior.
What Should Happen?
Hooks should either:
- Execute with an explicit
cwdthat's guaranteed to exist (e.g.,$HOMEor/tmp) as a fallback - Handle
ENOENTfromposix_spawngracefully and retry with a safe CWD - At minimum, log the actual CWD being used in the error message so the root cause is diagnosable
Error Messages/Logs
ENOENT: no such file or directory, posix_spawn '/bin/sh'
Steps to Reproduce
- Configure multiple Stop hooks in
~/.claude/settings.json(no sandbox configuration) - Use Claude Code normally
- Occasionally (not every time), Stop hooks fail with the above error
- Subsequent turns in the same session execute hooks successfully
Claude Model
Opus
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.1.42
Platform
Anthropic API
Operating System
Linux (Arch/CachyOS, kernel 6.18.8-3-cachyos)
Terminal/Shell
fish / Alacritty + tmux
Additional Information
Related to #20211 but distinct: that issue attributes the error to sandbox restrictions (Seatbelt on macOS). This report demonstrates the same error occurs on Linux without the sandbox feature enabled, pointing to a different root cause (likely CWD-related posix_spawn behavior).
This issue has 4 comments on GitHub. Read the full discussion on GitHub ↗