Execute Commands

Updated at:

Use the CLI to execute a command inside a running FC Agent Sandbox instance and return the output to the local terminal:

e2b sandbox exec <sandbox-id> <command>

For example, list the current directory in the sandbox:

e2b sandbox exec <sandbox-id> ls

The CLI also supports passing input from stdin, running in the background, setting a working directory, selecting a user, and defining environment variables:

echo "foo" | e2b sandbox exec <sandbox-id> <command>
e2b sandbox exec --background <sandbox-id> "sleep 60 && echo done"
e2b sandbox exec --cwd /home/user <sandbox-id> ls
e2b sandbox exec --user root <sandbox-id> apt-get update
e2b sandbox exec --env NODE_ENV=production --env DEBUG=true <sandbox-id> node app.js

If you need to execute commands in server-side code, use the SDK Commands API:

const result = await sandbox.commands.run("echo hello");
console.log(result.stdout.trim());

Python example:

result = sandbox.commands.run("echo hello")
print(result.stdout.strip())