Skip to main content
Execute and manage processes in your sandboxes with Blaxel SDK. Run shell commands, retrieve process information, and control process execution.
Complete code examples demonstrating all operations are available on Blaxel’s GitHub: in TypeScript, in Python, and in Go.

Execute processes and commands

The Blaxel SDK requires two environment variables to authenticate:
VariableDescription
BL_WORKSPACEYour Blaxel workspace name
BL_API_KEYYour Blaxel API key
You can create an API key from the Blaxel console. Your workspace name is visible in the URL when you log in to the console (e.g. app.blaxel.ai/{workspace}).Set them as environment variables or add them to a .env file at the root of your project:
export BL_WORKSPACE=my-workspace
export BL_API_KEY=my-api-key
The Blaxel SDK does not accept credentials as constructor arguments. Credentials must come from environment variables, a .env file, or a local CLI login session (see below).When developing locally, you can also log in to your workspace with Blaxel CLI (as shown above). This allows you to run Blaxel SDK functions that will automatically connect to your workspace without additional setup. When you deploy on Blaxel, authentication is handled automatically — no environment variables needed.

Execute command

Execute shell commands:
import { SandboxInstance } from "@blaxel/core";

const sandbox = await SandboxInstance.get("my-sandbox");

// Execute command
const process = await sandbox.process.exec({
  command: "echo 'Hello, World!'"
});
Logs for a process are available in the process execution object if the process is started with the waitForCompletion: true / "wait_for_completion": True parameter. Process execution logs are also visible in the Blaxel Console. Refer to the Logs section of the sandbox detail page, as shown below: process logs

Set the working directory

Set the working directory for subsequent commands:
import { SandboxInstance } from "@blaxel/core";

const sandbox = await SandboxInstance.get("my-sandbox");

// Execute command
const process = await sandbox.process.exec({
  workingDir: "/app",
  command: "ls -al"
});

Use process names

When starting a process (running a command), you can specify a process name. This lets you interact with the process—such as retrieving logs or process information—without needing to store the process ID on your end.
const process = await sandbox.process.exec({
  name: "hello-process",
  command: "echo 'Hello, World!'"
});
const processInfo = await sandbox.process.get("hello-process");
You can use either the process name or the process ID to get information about the process:
const completedProcess = await sandbox.process.get("hello-process");
if (completedProcess.status === "completed") {
  // ...
}
The exit code of the process is available in the exitCode parameter of the process execution object returned by the process.exec and process.get methods. You can also use the process ID or name to retrieve logs of your processes.

Kill process

Kill a process immediately by running:
await sandbox.process.kill("test")

Long-running processes

Blaxel Sandboxes are great for long-running processes. Processes you launch detach by design: their stdout and stderr are redirected to files and the process is monitored by background workers in the sandbox, so it survives independently of your client connection. By default, the general sandbox lifecycle rules still apply: the sandbox only stays awake while a connection is maintained and will suspend after a few seconds of inactivity. Use keepAlive to override this for the duration of a process. Two independent flags let you tune how the behavior works.
  • waitForCompletion (default: false) controls only whether the SDK call blocks until the process exits and returns its output. It has no effect on the process or sandbox lifetime.
  • keepAlive (default: none) controls only whether the sandbox stays awake (auto-standby/scale-to-zero disabled) while the process runs, even with no active client connection. A counter tracks running keepAlive processes and is decremented automatically when each one completes, letting the sandbox return to standby on its own. See Sandbox keep-alive for details.

Wait for process completion

You can wait for process completion when executing it:
const process = await sandbox.process.exec({
  name: "build-process",
  command: "npm run build",
  waitForCompletion: true,
  timeout: 60 // 60 seconds
});
When waiting for completion, the process execution object will contain a .logs field with the combined stdout and stderr output as a string. Also, notice the timeout parameter which allows you to set the process timeout duration in seconds.
When using waitForCompletion, Blaxel enforces a timeout limit of 60 seconds. Don’t set your timeout longer than this. For longer waiting periods, use the process-watching option described below.
For processes without keepAlive: true, the timeout parameter does not kill the process. It only bounds how long the API call waits when using waitForCompletion (or waitForPorts); once the timeout elapses, an error is returned but the process keeps running in the background. Non-keepAlive processes have no auto-kill timeout and run until they complete on their own. To auto-kill a process after a duration, use keepAlive: true together with a timeout (see Sandbox keep-alive).
You can also wait for a process after it has started:
await sandbox.process.exec({
  name: "long-task",
  command: "sleep 10"
});

// Wait for completion (max 10 minutes, check every 5 seconds)
await sandbox.process.wait("long-task", {
  maxWait: 600000,
  interval: 5000
});
Set a long completion duration if your process is expected to take longer.

Wait for ports

In some cases, you may want to wait for a port to be opened while running — for example if you are running npm run dev and want to wait for port 3000 to be open.
const process = await sandbox.process.exec({
  name: "dev-server",
  command: "npm run dev -- --port 3000 &",
  waitForPorts: [3000]
});

Auto-restart

Restart process on failure

You can restart a process if it fails, up to a maximum number of restart attempts:
const process = await sandbox.process.exec({
  name: "start-server",
  command: "npm run dev -- --host 0.0.0.0 --port 8000",
  restartOnFailure: true,
  maxRestarts: 5
});

Sandbox keep-alive

By default, sandboxes automatically switch to standby when there is no active connection to save resources. However, it is possible to adjust this behavior and keep the sandbox running when you launch a process, even if there isn’t an active connection to it. This is typically used to keep a long-running task actively run within the sandbox environment even when it is not serving requests. You can include additional keep-alive metadata when launching a new process:
import { SandboxInstance } from "@blaxel/core";

const sandbox = await SandboxInstance.get("my-sandbox");

// Start a dev server that keeps the sandbox awake
await sandbox.process.exec({
  name: "dev-server",
  command: "npm run dev -- --hostname 0.0.0.0 --port 3000",
  keepAlive: true,        // Prevents auto-standby while the process runs
  timeout: 3600           // Auto-kill the process after 1 hour. Set 0 to not auto-kill.
});
The auto-kill timeout and its 600-second default only apply to processes started with keepAlive: true. If a keepAlive process is started without a timeout, it defaults to 600 seconds (10 minutes) and is automatically terminated when the timeout elapses. To run a keepAlive process indefinitely with no auto-kill, set timeout: 0. The table below summarizes how keepAlive and timeout interact:
ConfigurationProcess auto-kill behaviortimeout parameter effect
keepAlive not set (default)Never auto-killed. Runs until it completes on its own.Only bounds how long the API waits when using waitForCompletion or waitForPorts. Does not terminate the process.
keepAlive: true, no timeoutAuto-killed after 600 seconds (10 minutes).
keepAlive: true, timeout: 0Never auto-killed. Runs until it completes on its own.
keepAlive: true, timeout: NAuto-killed after N seconds.
You can run multiple processes, and the sandbox will stay active until all of them complete:
import { SandboxInstance } from "@blaxel/core";

const sandbox = await SandboxInstance.get("my-sandbox");

await sandbox.process.exec({
  name: "api-server",
  command: "npm run start:api",
  keepAlive: true,
  timeout: 3600
});

await sandbox.process.exec({
  name: "worker",
  command: "npm run start:worker",
  keepAlive: true,
  timeout: 3600
});

Disable process telemetry logging

By default, process stdout and stderr are exported as structured telemetry logs (visible in external observability systems). You can disable this telemetry export by setting the SANDBOX_DISABLE_PROCESS_LOGGING environment variable inside the sandbox. When set to true or 1, structured telemetry output is suppressed for all processes. File-based logging and real-time log streaming via the SDK remain fully functional and are not affected. Set this variable in your Dockerfile or at sandbox creation time:
ENV SANDBOX_DISABLE_PROCESS_LOGGING=true
Alternatively, you can disable process logging for sandboxes at workspace level using the Blaxel Console, in the Workspace settings section. Disable process logging
This only suppresses the structured telemetry export. You can still retrieve process logs via the SDK’s process.logs() method and stream them in real-time.

Process statuses

A process can have either of the following status:
  • running
  • completed
  • failed
  • killed
  • stopped
Last modified on June 27, 2026