Skip to content
DocsAll docs
Console

Commands

Run a command in a sandbox and read its output and exit code.

Last updated

On this page

Description and operation

General

A command is the main process of a sandbox. You give it at creation as cmd: the program, then its arguments. The sandbox ends when the command exits.

ItemValue
CommandThe program and its arguments, 256 arguments and 256 KiB at most.
Working directory~/workspace.
OutputThe output and the error output of the command, together.
Output keptThe live output while it runs, then the last 256 KiB for 90 days.
Exit codeThe code of the command, in exit_code. The reason is Exited.
One waitAt most 20 seconds. The SDK waits in steps.

The command can start other processes. The sandbox waits only for the command. When the command exits, the other processes end with the sandbox.

For one command, sandboxes.run creates the sandbox, waits for the command, reads the output and ends the sandbox. It returns the exit code, the output and the id.

A command can use the name of a service when the sandbox is created in a session with an environment. See Sandboxes.

Tasks

Run a command

Reason for the task: Run a command in a new sandbox and read the output and the exit code.

Job set-up

WARNING

The files of a sandbox are lost when it ends. Write the result to the output, or take a snapshot, before the command exits.

Procedure

  1. Create the sandbox with the command. The call returns at once.

    Python
    sb = kl.sandboxes.create(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])
    TypeScript
    const sb = await kl.sandboxes.create({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });
    Go
    sb, err := kl.Sandboxes.Create(kloudlite.CreateSandbox{Cmd: []string{"python3", "-c", "print(6 * 7)"}, Packages: []string{"python3"}})if err != nil {    log.Fatal(err)}
    Ruby
    sb = kl.sandboxes.create(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])
    Java
    var sb = kl.sandboxes().create(new Kloudlite.CreateSandbox().cmd(List.of("python3", "-c", "print(6 * 7)")).packages("python3"));
    CLI
    kloudlite sandboxes create --package python3 -- python3 -c 'print(6 * 7)'
  2. Wait for the command to exit. The call returns the exit code.

    Python
    code = sb.wait()
    TypeScript
    const code = await sb.wait();
    Go
    code, err := sb.Wait(0)if err != nil {    log.Fatal(err)}
    Ruby
    code = sb.wait
    Java
    int code = sb.waitFor(null);
    CLI
    kloudlite sandboxes wait sbx-3c4d5e6f7a8b9c0d
  3. Read the output.

    Python
    print(sb.logs())
    TypeScript
    console.log(await sb.logs());
    Go
    out, err := sb.Logs(false)if err != nil {    log.Fatal(err)}fmt.Print(out)
    Ruby
    puts sb.logs
    Java
    System.out.print(sb.logs(false));
    CLI
    kloudlite sandboxes logs sbx-3c4d5e6f7a8b9c0d

Close-out

  1. Make sure that the output shows 42 and the exit code is 0.
  2. Delete the record if you do not need it, as in End a sandbox.

To run one command in one call, use run. The call ends the sandbox for you.

Python
res = kl.sandboxes.run(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])print(res["exit_code"], res["output"])
TypeScript
const res = await kl.sandboxes.run({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });console.log(res.exitCode, res.output);
Go
res, err := kl.Sandboxes.Run(kloudlite.RunSandbox{Sandbox: kloudlite.CreateSandbox{Cmd: []string{"python3", "-c", "print(6 * 7)"}, Packages: []string{"python3"}}})if err != nil {	log.Fatal(err)}fmt.Println(res.ExitCode, res.Output)
Ruby
res = kl.sandboxes.run(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])puts res["exit_code"], res["output"]
Java
var res = kl.sandboxes().run(new Kloudlite.RunSandbox().cmd(List.of("python3", "-c", "print(6 * 7)")).packages("python3"));System.out.println(res.exitCode + " " + res.output);
CLI
kloudlite sandboxes run --package python3 -- python3 -c 'print(6 * 7)'

Troubleshooting

SymptomPossible causeCorrective action
422 for cmdThe command is empty or too long.Give the program and at most 256 arguments.
reason is Exited and exit_code is not 0The command failed.Read the output with logs.
reason is Timeout and exit_code is emptyThe command ran longer than timeout_s.Create a new sandbox with a larger timeout_s.
409 with has no output yet (starting)The sandbox has not started.Wait, then read the output again.
The output is shorter than expectedThe platform keeps the last 256 KiB after the end.Read the output with follow while the command runs.
A file that the command wrote is goneThe files of a sandbox are lost when it ends.Write the result to the output.