2025-12-25 00:25:11 +00:00
---
2026-01-12 02:49:55 +00:00
summary: "Background exec execution and process management"
2025-12-25 00:25:11 +00:00
read_when:
2026-01-12 02:49:55 +00:00
- Adding or modifying background exec behavior
- Debugging long-running exec tasks
2026-01-31 16:04:03 -05:00
title: "Background Exec and Process Tool"
2025-12-25 00:25:11 +00:00
---
2026-01-12 02:49:55 +00:00
# Background Exec + Process Tool
2025-12-25 00:25:11 +00:00
2026-01-30 03:15:10 +01:00
OpenClaw runs shell commands through the `exec` tool and keeps long‑ running tasks in memory. The `process` tool manages those background sessions.
2025-12-25 00:25:11 +00:00
2026-01-12 02:49:55 +00:00
## exec tool
2025-12-25 00:25:11 +00:00
Key parameters:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
- `command` (required)
2026-01-03 20:15:02 +00:00
- `yieldMs` (default 10000): auto‑ background after this delay
2025-12-25 00:25:11 +00:00
- `background` (bool): background immediately
2025-12-25 17:58:19 +00:00
- `timeout` (seconds, default 1800): kill the process after this timeout
2026-04-04 18:39:12 +01:00
- `elevated` (bool): run outside the sandbox if elevated mode is enabled/allowed (`gateway` by default, or `node` when the exec target is `node` )
2026-01-17 04:57:04 +00:00
- Need a real TTY? Set `pty: true` .
2025-12-25 00:25:11 +00:00
- `workdir` , `env`
Behavior:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
- Foreground runs return output directly.
- When backgrounded (explicit or timeout), the tool returns `status: "running"` + `sessionId` and a short tail.
- Output is kept in memory until the session is polled or cleared.
2026-01-12 02:49:55 +00:00
- If the `process` tool is disallowed, `exec` runs synchronously and ignores `yieldMs` /`background` .
2026-03-01 20:31:06 -08:00
- Spawned exec commands receive `OPENCLAW_SHELL=exec` for context-aware shell/profile rules.
2026-04-04 20:49:15 +01:00
- For long-running work that starts now, start it once and rely on automatic
completion wake when it is enabled and the command emits output or fails.
- If automatic completion wake is unavailable, or you need quiet-success
confirmation for a command that exited cleanly without output, use `process`
to confirm completion.
- Do not emulate reminders or delayed follow-ups with `sleep` loops or repeated
polling; use cron for future work.
2025-12-25 00:25:11 +00:00
2026-01-15 06:58:46 +00:00
## Child process bridging
When spawning long-running child processes outside the exec/process tools (for example, CLI respawns or gateway helpers), attach the child-process bridge helper so termination signals are forwarded and listeners are detached on exit/error. This avoids orphaned processes on systemd and keeps shutdown behavior consistent across platforms.
2025-12-25 00:25:11 +00:00
Environment overrides:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
- `PI_BASH_YIELD_MS` : default yield (ms)
- `PI_BASH_MAX_OUTPUT_CHARS` : in‑ memory output cap (chars)
2026-01-30 03:15:10 +01:00
- `OPENCLAW_BASH_PENDING_MAX_OUTPUT_CHARS` : pending stdout/stderr cap per stream (chars)
2025-12-25 00:25:11 +00:00
- `PI_BASH_JOB_TTL_MS` : TTL for finished sessions (ms, bounded to 1m– 3h)
2025-12-25 17:58:19 +00:00
Config (preferred):
2026-01-31 21:13:13 +09:00
2026-01-12 02:49:55 +00:00
- `tools.exec.backgroundMs` (default 10000)
- `tools.exec.timeoutSec` (default 1800)
- `tools.exec.cleanupMs` (default 1800000)
2026-01-31 21:13:13 +09:00
- `tools.exec.notifyOnExit` (default true): enqueue a system event + request heartbeat when a backgrounded exec exits.
2026-02-14 18:36:35 -05:00
- `tools.exec.notifyOnExitEmptySuccess` (default false): when true, also enqueue completion events for successful backgrounded runs that produced no output.
2025-12-25 17:58:19 +00:00
2025-12-25 00:25:11 +00:00
## process tool
Actions:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
- `list` : running + finished sessions
- `poll` : drain new output for a session (also reports exit status)
- `log` : read the aggregated output (supports `offset` + `limit` )
- `write` : send stdin (`data` , optional `eof` )
2026-04-04 20:49:15 +01:00
- `send-keys` : send explicit key tokens or bytes to a PTY-backed session
- `submit` : send Enter / carriage return to a PTY-backed session
- `paste` : send literal text, optionally wrapped in bracketed paste mode
2025-12-25 00:25:11 +00:00
- `kill` : terminate a background session
- `clear` : remove a finished session from memory
- `remove` : kill if running, otherwise clear if finished
Notes:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
- Only backgrounded sessions are listed/persisted in memory.
- Sessions are lost on process restart (no disk persistence).
- Session logs are only saved to chat history if you run `process poll/log` and the tool result is recorded.
2026-01-07 23:35:04 +01:00
- `process` is scoped per agent; it only sees sessions started by that agent.
2026-04-04 20:49:15 +01:00
- Use `poll` / `log` for status, logs, quiet-success confirmation, or
completion confirmation when automatic completion wake is unavailable.
- Use `write` / `send-keys` / `submit` / `paste` / `kill` when you need input
or intervention.
2025-12-25 17:58:19 +00:00
- `process list` includes a derived `name` (command verb + target) for quick scans.
2026-02-14 18:36:35 -05:00
- `process log` uses line-based `offset` /`limit` .
- When both `offset` and `limit` are omitted, it returns the last 200 lines and includes a paging hint.
- When `offset` is provided and `limit` is omitted, it returns from `offset` to the end (not capped to 200).
2026-04-04 20:49:15 +01:00
- Polling is for on-demand status, not wait-loop scheduling. If the work should
happen later, use cron instead.
2025-12-25 00:25:11 +00:00
## Examples
Run a long task and poll later:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
``` json
2026-01-31 21:13:13 +09:00
{ "tool" : "exec" , "command" : "sleep 5 && echo done" , "yieldMs" : 1000 }
2025-12-25 00:25:11 +00:00
```
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
``` json
2026-01-31 21:13:13 +09:00
{ "tool" : "process" , "action" : "poll" , "sessionId" : "<id>" }
2025-12-25 00:25:11 +00:00
```
Start immediately in background:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
``` json
2026-01-31 21:13:13 +09:00
{ "tool" : "exec" , "command" : "npm run build" , "background" : true }
2025-12-25 00:25:11 +00:00
```
Send stdin:
2026-01-31 21:13:13 +09:00
2025-12-25 00:25:11 +00:00
``` json
2026-01-31 21:13:13 +09:00
{ "tool" : "process" , "action" : "write" , "sessionId" : "<id>" , "data" : "y\n" }
2025-12-25 00:25:11 +00:00
```
2026-04-04 20:49:15 +01:00
Send PTY keys:
``` json
{ "tool" : "process" , "action" : "send-keys" , "sessionId" : "<id>" , "keys" : [ "C-c" ] }
```
Submit current line:
``` json
{ "tool" : "process" , "action" : "submit" , "sessionId" : "<id>" }
```
Paste literal text:
``` json
{ "tool" : "process" , "action" : "paste" , "sessionId" : "<id>" , "text" : "line1\nline2\n" }
```