Skip to content

Background commands ​

Set background in JavaScript/Python or call Commands.Start in Go to start a command without waiting for it. The returned handle contains the process ID and can wait for, disconnect from, or stop the command.

mjs
import { Sandbox } from "@abox-dev/sdk";

const sandbox = await Sandbox.create();

try {
  const command = await sandbox.commands.run(
    "sleep 1; printf 'job finished' > /tmp/job.txt",
    { background: true, timeoutMs: 0 },
  );

  const pid = command.pid;
  await command.disconnect();

  const reconnected = await sandbox.commands.connect(pid);
  await reconnected.wait();
  console.log(await sandbox.files.read("/tmp/job.txt"));
} finally {
  await sandbox.kill();
}

Save both the sandbox ID and process ID when another application process will collect the result. Connect to the sandbox first, then call sandbox.commands.connect(pid).

Output callbacks belong to the current connection. In Go, use ConnectWithOptions to configure callbacks and streaming when reconnecting. Close detaches the handle and leaves the process running. Write important output to a file when it must remain available after disconnecting. Use timeoutMs: 0 in JavaScript or timeout=0 in Python for a command that must not have its own execution timeout. In Go, the attachment context controls the local stream. Canceling it detaches without killing the remote process. Keep the sandbox timeout long enough for the job in every language.