# how-to-run-cli > Run CLI tools like Claude Code and Opencode in OpenClaw's non-interactive environment. Use when executing AI CLI tools (claude, opencode) that hang or fail in automated contexts. Required for any task involving running Claude Code or Opencode commands from OpenClaw. - Author: Mark Berjanskii - Repository: mberjans/openclaw-skills - Version: 20260209041332 - Stars: 0 - Forks: 0 - Last Updated: 2026-02-09 - Source: https://github.com/mberjans/openclaw-skills - Web: https://mule.run/skillshub/@@mberjans/openclaw-skills~how-to-run-cli:20260209041332 --- --- name: how-to-run-cli description: Run CLI tools like Claude Code and Opencode in OpenClaw's non-interactive environment. Use when executing AI CLI tools (claude, opencode) that hang or fail in automated contexts. Required for any task involving running Claude Code or Opencode commands from OpenClaw. --- # How To Run CLI Tools in OpenClaw ## Overview This skill provides the correct method for running interactive CLI tools (Claude Code, Opencode) in OpenClaw's non-interactive environment. These tools hang indefinitely when run normally because they expect interactive terminal sessions. ## The Problem CLI tools like Claude Code and Opencode work fine in interactive SSH sessions but hang forever when run via OpenClaw's `exec` tool or Python subprocess. They wait for interactive prompts (API key confirmation, permissions) that never arrive. ## The Solution Use `setsid` to create a new session and redirect stdin from `/dev/null`: ```bash bash -c 'cd /home/openclaw && TERM=dumb setsid /path/to/cli-tool --flags -p "prompt" &1' ``` ### Required Components 1. **`setsid`** - Creates a new session, detaching from parent terminal 2. **`&1`** - Captures both stdout and stderr ## Quick Reference ### Claude Code ```bash bash -c 'cd /home/openclaw && TERM=dumb setsid /home/openclaw/.local/bin/claude --model glm-4.7 --dangerously-skip-permissions -p "Your prompt here" &1' ``` **Optional flags:** - `--output-format json` - Structured JSON output - `--max-turns N` - Limit turns (default: 3) ### Opencode ```bash bash -c 'cd /home/openclaw && TERM=dumb setsid /home/openclaw/.opencode/bin/opencode run -m opencode/kimi-k2.5-free "Your prompt" &1' ``` ## Common Issues | Issue | Cause | Solution | |-------|-------|----------| | Hanging, no output | Missing `setsid` or `&1'""" result = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=60) print(result.stdout) ``` ## Resources ### references/ - **[cli-running-guide.md](references/cli-running-guide.md)** - Detailed guide with examples and troubleshooting