Claude Code Runner
Overview
A wrapper skill for running Claude Code programmatically in non-interactive environments. Uses PTY (pseudo-terminal) to handle TTY-required operations and automatically responds to confirmation prompts.
Features
- - PTY-based execution: Works in non-TTY environments (containers, CI/CD, background processes)
- Auto-respond to prompts: Automatically answers "Do you want to..." confirmations
- User switching: Runs as specified non-root user
- File synchronization: Copies project to temp directory, executes, syncs changes back
- Timeout handling: Configurable timeout with proper cleanup
- Output capture: Captures and returns full stdout/stderr
Installation
CODEBLOCK0
Usage
Basic Usage
CODEBLOCK1
Via Command Line
CODEBLOCK2
Advanced Options
CODEBLOCK3
API Reference
run_claude_code(workdir, prompt, user='lighthouse', timeout=300)
Execute a Claude Code task in a PTY environment.
Parameters:
- -
workdir (str): Working directory containing the project - INLINECODE2 (str): Natural language task description
- INLINECODE3 (str): User to run as (default: 'lighthouse')
- INLINECODE4 (int): Timeout in seconds (default: 300)
Returns:
- -
str: Combined stdout and stderr output
Behavior:
- 1. Copies project to temporary directory
- Changes ownership to specified user
- Executes Claude Code via PTY
- Auto-responds to confirmation prompts
- Syncs changes back to original directory
- Cleans up temporary files
Use Cases
1. Automated Code Review
CODEBLOCK4
2. Refactoring Tasks
CODEBLOCK5
3. Adding Features
CODEBLOCK6
4. Bug Fixes
CODEBLOCK7
Requirements
- - Python 3.8+
- Claude Code installed and in PATH
- Unix-like environment (Linux/macOS)
- Root or sudo access (for user switching)
Configuration
Environment Variables
- -
CLAUDE_CODE_USER: Default user to run as (default: 'lighthouse') - INLINECODE7 : Default timeout in seconds (default: 300)
Customization
Edit scripts/run_claude.py to customize:
- - Auto-response keywords
- Temp directory location
- Sync behavior
- Output formatting
Troubleshooting
"Permission denied" errors
Ensure the script is run with sufficient privileges to:
- - Create temporary directories
- Change file ownership
- Switch to target user
Claude Code not found
Make sure Claude Code is installed and in the system PATH:
CODEBLOCK8
Task timeout
Increase the timeout for long-running tasks:
CODEBLOCK9
Interactive prompts not auto-responded
Add new prompt patterns to the auto-respond logic:
CODEBLOCK10
Limitations
- - Requires Unix-like environment (uses PTY)
- Requires root/sudo for user switching
- Claude Code must be installed separately
- May not handle all edge cases of interactive prompts
License
MIT License
Copyright (c) 2026 lhl09120
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
See LICENSE file for full details.
Changelog
v1.0.0 (2026-02-27)
- - Initial release
- PTY-based Claude Code execution
- Auto-response to confirmation prompts
- File synchronization
- User switching support
Claude 代码运行器
概述
一个用于在非交互式环境中以编程方式运行 Claude Code 的封装技能。使用 PTY(伪终端)处理需要 TTY 的操作,并自动响应确认提示。
特性
- - 基于 PTY 的执行:可在非 TTY 环境(容器、CI/CD、后台进程)中工作
- 自动响应提示:自动回答是否要...等确认信息
- 用户切换:以指定的非 root 用户身份运行
- 文件同步:将项目复制到临时目录,执行操作,同步更改回原目录
- 超时处理:可配置超时时间,并妥善清理资源
- 输出捕获:捕获并返回完整的标准输出和标准错误
安装
bash
克隆技能
git clone https://github.com/lhl09120/claude-code-runner-en.git
赋予脚本执行权限
chmod +x claude-code-runner-en/scripts/run_claude.py
使用方法
基本用法
python
from claudecoderunner import runclaudecode
result = runclaudecode(
workdir=/path/to/project,
prompt=重构认证模块以使用 JWT 令牌,
user=lighthouse,
timeout=300
)
print(result)
通过命令行使用
bash
python3 scripts/run_claude.py /path/to/project 在此输入任务描述
高级选项
python
result = runclaudecode(
workdir=/root/repo/my-project,
prompt=
1. 审查代码库
2. 识别安全漏洞
3. 修复发现的问题
4. 添加适当的测试
,
user=developer,
timeout=600 # 10 分钟
)
API 参考
runclaudecode(workdir, prompt, user=lighthouse, timeout=300)
在 PTY 环境中执行 Claude Code 任务。
参数:
- - workdir (str):包含项目的工作目录
- prompt (str):自然语言的任务描述
- user (str):运行任务的用户(默认:lighthouse)
- timeout (int):超时时间(秒)(默认:300)
返回值:
行为:
- 1. 将项目复制到临时目录
- 将所有权更改为指定用户
- 通过 PTY 执行 Claude Code
- 自动响应确认提示
- 将更改同步回原始目录
- 清理临时文件
使用场景
1. 自动化代码审查
python
result = runclaudecode(
workdir=/root/repo/project,
prompt=审查此代码库,识别潜在的错误或改进点
)
2. 重构任务
python
result = runclaudecode(
workdir=/root/repo/legacy-app,
prompt=重构数据库层,使用 SQLAlchemy ORM 替代原始 SQL
)
3. 添加功能
python
result = runclaudecode(
workdir=/root/repo/api-service,
prompt=
为用户资料管理添加新的 REST 端点:
- GET /api/users/{id}/profile
- PUT /api/users/{id}/profile
- 包含验证和错误处理
- 添加单元测试
)
4. 修复 Bug
python
result = runclaudecode(
workdir=/root/repo/web-app,
prompt=修复 WebSocket 连接处理程序中的内存泄漏
)
要求
- - Python 3.8+
- 已安装 Claude Code 并在 PATH 中
- Unix 类环境(Linux/macOS)
- Root 或 sudo 访问权限(用于用户切换)
配置
环境变量
- - CLAUDECODEUSER:默认运行用户(默认:lighthouse)
- CLAUDECODETIMEOUT:默认超时时间(秒)(默认:300)
自定义配置
编辑 scripts/run_claude.py 以自定义:
故障排除
权限被拒绝错误
确保脚本以足够的权限运行,以便:
未找到 Claude Code
确保 Claude Code 已安装并在系统 PATH 中:
bash
which claude
任务超时
为长时间运行的任务增加超时时间:
python
runclaudecode(workdir, prompt, timeout=600) # 10 分钟
交互式提示未自动响应
向自动响应逻辑添加新的提示模式:
python
if b新的提示文本 in output:
os.write(master_fd, by\n)
局限性
- - 需要 Unix 类环境(使用 PTY)
- 需要 root/sudo 权限进行用户切换
- Claude Code 必须单独安装
- 可能无法处理所有交互式提示的边缘情况
许可证
MIT 许可证
版权所有 (c) 2026 lhl09120
特此免费授予任何获得本软件及相关文档文件(软件)副本的人,不受限制地处理本软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或销售本软件副本的权利,并允许获得本软件的人这样做,但须满足以下条件:
上述版权声明和本许可声明应包含在本软件的所有副本或实质性部分中。
详见 LICENSE 文件获取完整信息。
更新日志
v1.0.0(2026-02-27)
- - 初始版本发布
- 基于 PTY 的 Claude Code 执行
- 自动响应确认提示
- 文件同步
- 用户切换支持