> ## Documentation Index
> Fetch the complete documentation index at: https://novita.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Execute commands in sandbox

export const SandboxCliConfigHint = () => {
  if (typeof document === "undefined") {
    return null;
  } else {
    return <Note>Before running the command-line related example code in this document, please refer to the <Link href="/guides/sandbox-cli">tutorial</Link> to install the CLI and complete <Link href="/guides/sandbox-cli-auth">authentication</Link>.</Note>;
  }
};

<SandboxCliConfigHint />

You can execute commands in a running sandbox and receive its output by using the following command.

```bash Bash icon="terminal" theme={"system"}
novita-sandbox-cli sandbox exec <sandbox-id> <command>
```

### Execution options

`--background` flag lets you run a command in the background and return immediately. The command will print the process ID (PID) to stderr:

```bash Bash icon="terminal" theme={"system"}
novita-sandbox-cli sandbox exec --background <sandbox-id> "sleep 60 && echo done"
```

`--cwd` flag specifies the working directory for the command.

```bash Bash icon="terminal" theme={"system"}
novita-sandbox-cli sandbox exec --cwd /home/user <sandbox-id> ls
```

`--user` flag specifies the user to run the command.

```bash Bash icon="terminal" theme={"system"}
novita-sandbox-cli sandbox exec --user root <sandbox-id> apt-get update
```

`--env` flag lets you set environment variables. Repeat the flag to set multiple variables.

```bash Bash icon="terminal" theme={"system"}
novita-sandbox-cli sandbox exec --env NODE_ENV=production --env DEBUG=true <sandbox-id> node app.js
```
