# Use the command line

Create, run, and drive machines with `osmates` in Terminal or a script.

Every command and option is in the [Commands reference](https://osmates.com/docs/cli#commands).

## Set up Terminal

1. [Add the command line](https://osmates.com/docs/install#add-the-command-line).
2. Open a new Terminal window, then run `osmates --version`.

Add `--help` to any command for its options. For an AI agent's shell, use [`osmates agent`](https://osmates.com/docs/agent-cli) instead.

## Create and start a machine

To list the images, create `dev` from the default one, and start it:

```sh
osmates vm images
osmates vm create dev
osmates vm start dev
```

[Start a machine](https://osmates.com/docs/power#start-a-machine) covers `--no-window` and `--queue`.

Commands take a machine's name or its identifier. `osmates vm list` lists your machines, and `osmates vm info dev` shows one. With `--json`, both give sizes in bytes.

To continue a creation that stopped, see [If creating a machine stops](https://osmates.com/docs/create-machine#if-creating-a-machine-stops). To delete a machine with its disk and snapshots, which you can't undo, run `osmates vm delete dev`.

## Run a program

Put the program after `--`:

```sh
osmates vm exec dev -- uname -a
```

Add `--desktop` to run it as the user signed in at the screen. Control-C stops the program. The command exits with the program's status, as [Read the exit status](https://osmates.com/docs/api#read-the-exit-status) explains.

For long work, start a job with `--detach`, which prints a job identifier. Replace `<job>` with it to follow the job:

```sh
osmates vm exec --detach dev -- make -C /srv
osmates job status <job> --wait
osmates job output <job> --follow
```

`osmates job cancel <job>` stops a job, and `osmates job list` shows them all. Finished jobs stay for an hour.

## Watch a machine

```sh
osmates vm events dev --follow
osmates vm logs dev --source console
osmates vm text dev
osmates vm wait dev --until stable
```

`vm text` reads the text on screen, with a bounding box for each piece.

## Click and type in a machine

1. Run `osmates vm screenshot dev --output screen.png`. It prints a frame identifier.
2. Read the coordinates from the image, in pixels from the top-left corner.
3. Click or type, with the frame identifier for `<frame>`:

```sh
osmates vm click dev 250 150 --frame <frame>
osmates vm type dev "hello\n"
osmates vm key dev ctrl+c
```

The frame stops a click from landing after the display changed or the machine restarted. If the click is refused, take a new screenshot.

To act on a control by its reference instead, read the controls first. Wayland desktops return controls without positions.

```sh
osmates vm elements dev --mode interactive
osmates vm act dev <reference> set-value "hello"
```

## If a command fails

An error lists commands that may fix it. For a failed machine, `osmates vm info dev` lists recovery steps.

If the error names another copy of the app, run the command it gives. Running the same command again fails.

To check an unfinished update, run `osmates status`, then see [Update OSMates](https://osmates.com/docs/update).
