File contents Procedure
基本形 : claude -p "<prompt>" --output-format text で stdout に結果
timeout 付き (macOS は gtimeout 推奨, なければ perl alarm):
perl -e 'alarm shift; exec @ARGV' 300 claude -p "..." || echo "timeout"
MAX_THINKING_TOKENS 抑制 : 大型 prompt なら MAX_THINKING_TOKENS=10000 claude -p ...
stderr 分離 : claude -p "..." 2> /tmp/claude-err.log で error trace 別保存
stdin pipe : cat input.md | claude -p "summarize" でファイル入力
launchd / 自動化から呼ぶなら MCP と hook を切る(必須) : 文章生成・判定だけならツールは要らない。
claude -p --model haiku --strict-mcp-config --mcp-config '{"mcpServers":{}}' --allowedTools ''
--mcp-config '{}' は mcpServers: Invalid input で即死する。必ず {"mcpServers":{}} 。
Pitfalls
system prompt 全ロードで cost 高 → 短プロンプトでも 5-50 cents / call
session 切れ (8:50am reset) で中断 → claude --resume で復旧
--output-format text 指定しないと JSON 返ってきて parse 必要
macOS timeout コマンドは GNU coreutils 必要 → brew install coreutils で gtimeout 提供
並列 claude -p は token block を急速消費 → 5h 800k cap 注意
launchd 下で毎回タイムアウトする(自前 timeout の SIGTERM = exit 143)のは MCP サーバ/hook の起動待ちが原因 。
対話セッションでは10秒で終わる同じ生成が、細い env + 高負荷だと数分かかる。stderr は空なので原因が見えない。
上の Procedure 6 で殺す+タイムアウト自体も余裕を持たせる(90秒→180秒)。実測 6.5 秒に短縮(2026-07-25 note/social 自動返信の全滅を根治)
Verification
exit 0 + stdout 非空 = 成功
exit 124 (timeout) または非ゼロ = 失敗
ccusage blocks --active --json で block 残量を呼び出し後に確認
1 --- 2 name: claude-headless-p-invocation 3 description: Procedure 4 --- 5 6 ## Procedure 7 8 1. **基本形**: `claude -p "<prompt>" --output-format text` で stdout に結果 9 2. **timeout 付き** (macOS は gtimeout 推奨, なければ perl alarm): 10 ```bash 11 perl -e 'alarm shift; exec @ARGV' 300 claude -p "..." || echo "timeout" 12 ``` 13 3. **MAX_THINKING_TOKENS 抑制**: 大型 prompt なら `MAX_THINKING_TOKENS=10000 claude -p ...` 14 4. **stderr 分離**: `claude -p "..." 2> /tmp/claude-err.log` で error trace 別保存 15 5. **stdin pipe**: `cat input.md | claude -p "summarize"` でファイル入力 16 6. **launchd / 自動化から呼ぶなら MCP と hook を切る(必須)**: 文章生成・判定だけならツールは要らない。 17 ```bash 18 claude -p --model haiku --strict-mcp-config --mcp-config '{"mcpServers":{}}' --allowedTools '' 19 ``` 20 `--mcp-config '{}'` は `mcpServers: Invalid input` で即死する。**必ず `{"mcpServers":{}}`**。 21 22 ## Pitfalls 23 24 - system prompt 全ロードで cost 高 → 短プロンプトでも 5-50 cents / call 25 - session 切れ (8:50am reset) で中断 → claude --resume <session-id> で復旧 26 - `--output-format text` 指定しないと JSON 返ってきて parse 必要 27 - macOS `timeout` コマンドは GNU coreutils 必要 → `brew install coreutils` で `gtimeout` 提供 28 - 並列 claude -p は token block を急速消費 → 5h 800k cap 注意 29 - **launchd 下で毎回タイムアウトする(自前 timeout の SIGTERM = exit 143)のは MCP サーバ/hook の起動待ちが原因**。 30 対話セッションでは10秒で終わる同じ生成が、細い env + 高負荷だと数分かかる。stderr は空なので原因が見えない。 31 上の Procedure 6 で殺す+タイムアウト自体も余裕を持たせる(90秒→180秒)。実測 6.5 秒に短縮(2026-07-25 note/social 自動返信の全滅を根治) 32 33 ## Verification 34 35 - exit 0 + stdout 非空 = 成功 36 - exit 124 (timeout) または非ゼロ = 失敗 37 - `ccusage blocks --active --json` で block 残量を呼び出し後に確認
bokuwalily/claude-code-skills/tree/main/skills/claude-headless-p-invocation commit b2035c6f02
Frequently asked questions How do I install the Claude Headless P Invocation skill? Run npx skillmds@latest add bokuwalily/claude-headless-p-invocation in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the Claude Headless P Invocation skill do? Procedure It is listed under Coding & Dev Tools on SkillMD.
Is Claude Headless P Invocation safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with Claude Headless P Invocation? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is Claude Headless P Invocation free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published Claude Headless P Invocation? bokuwalily (@bokuwalily) published this skill. Their other Agent Skills are listed on their SkillMD profile.