# Run commands and long tasks

Two ways to run a program on a machine: `exec_run` waits for it and returns its exit code, and
`task_start` starts it in a terminal that keeps running after your call or your conversation ends.

**In short:** One-shot: `exec_run {"argv": ["git", "status"], "cwd": "~/project"}`. Long or interactive:
`task_start {"command": "npm run dev"}`, then `task_output {"task": <id>}`, then `task_stop`.

## Before you start

Both need the `exec` class in your grant and in the machine's ceiling. Without it the call returns
[`denied`](https://docs.cmdop.ai/reference/errors/denied.md): tell the owner, do not retry.

## Run a command and wait

1. Call [`exec_run`](https://docs.cmdop.ai/reference/tools/exec_run.md) with `argv` (run directly, no shell) or `command`
   (run as `/bin/sh -c`):

   ```json
   { "machine": "build-box", "argv": ["cargo", "test", "--quiet"], "cwd": "~/src/app", "timeout_secs": 300 }
   ```

2. Read the result: `exit_code`, `stdout` and `stderr` apart, and `timed_out`. A non-zero exit is a
   normal result, not an error: decide from the code and the output.

Limits:

- `timeout_secs` defaults to 120, at most 600. On timeout the whole process group is stopped.
- Each stream keeps its last 64 KiB; longer output is saved to the files named in `stdout_file`
  and `stderr_file`, which you can read with [`fs_read`](https://docs.cmdop.ai/reference/tools/fs_read.md).
- The command starts with a small base environment (`PATH`, `HOME`, `USER`, `SHELL`, the locale,
  the desktop session) plus what you pass in `env`. Nothing else from the machine program's own
  environment reaches it, so a key set there is not passed on.

## Start a long task

For a dev server, a build that takes longer than ten minutes, or anything interactive:

1. Call [`task_start`](https://docs.cmdop.ai/reference/tools/task_start.md) with `argv` or `command`, and `cwd`. It returns
   the task id at once.
2. Read the task's screen with [`task_output`](https://docs.cmdop.ai/reference/tools/task_output.md)
   `{"task": <id>}`: the last 200 lines by default, `lines: 0` for all the machine keeps. Call it
   again to follow progress; it works whether the task still runs or has ended.
3. End it with [`task_stop`](https://docs.cmdop.ai/reference/tools/task_stop.md): the machine sends SIGTERM to its process
   group, and SIGKILL after 5 seconds.

Lost the id? Over MCP, `tasks_list` returns everything running now, by author
([MCP server](https://docs.cmdop.ai/reference/mcp.md#tasks_list)).

What a task survives, and what it does not:

| Event | The task |
|---|---|
| your call or conversation ends, the page closes, the network drops | keeps running; reconnect and read it |
| the machine is paused | is stopped, with every other task |
| the machine program restarts | is stopped; it is listed as lost |

The machine keeps 1000 lines of output of its last 50 tasks until it restarts.

## Check

`exec_run` returns `exit_code: 0` for the command you expected to succeed. For a task,
`task_output` shows the program's own output, and `tasks_list` lists it under your name.

## If it fails

- [`busy`](https://docs.cmdop.ai/reference/errors/busy.md): the machine already runs 8 calls at once. Wait and retry.
- [`paused`](https://docs.cmdop.ai/reference/errors/paused.md): the owner paused the machine; stop and tell them.
- [`timed_out`](https://docs.cmdop.ai/reference/errors/timed_out.md): the call did not finish in time. For long work use a
  task instead.

## Related

[Tasks](https://docs.cmdop.ai/concepts/tasks.md) · [Risk classes](https://docs.cmdop.ai/concepts/risk-classes.md) ·
[Work with files](https://docs.cmdop.ai/guides/files.md)
