Kill Commands

Updated at:

Killing a command stops a background process or PTY session that is running in the sandbox. It does not terminate the entire sandbox.

Kill a background command

const handle = await sandbox.commands.run("sleep 600", {
  background: true,
  timeoutMs: 10 * 60 * 1000
});

const killed = await sandbox.commands.kill(handle.pid);
console.log(killed);

Python example:

handle = sandbox.commands.run(
    "sleep 600",
    background=True,
    timeout=10 * 60,
)

killed = sandbox.commands.kill(handle.pid)
print(killed)

Clean up background commands

const commandPids = [handle.pid];

for (const pid of commandPids) {
  await sandbox.commands.kill(pid);
}

Python example:

command_pids = [handle.pid]

for pid in command_pids:
    sandbox.commands.kill(pid)

Your application should persist the pid of the background commands it starts. sandbox.commands.list() returns running commands and PTY sessions. Use sandbox.commands.kill(pid) for regular commands and sandbox.pty.kill(pid) for PTY sessions. Distinguish them by process purpose or the pid values you persisted, so you do not treat interactive sessions as ordinary background commands.

Recommendations

Long-running tasks should have application-side timeouts and actively kill commands after a timeout is reached. Killing a command only releases process resources. When the full task is finished, also call sandbox.kill() to release the sandbox.