Commands
Run a command in a sandbox and read its output and exit code.
Last updated
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.
| Item | Value |
|---|---|
| Command | The program and its arguments, 256 arguments and 256 KiB at most. |
| Working directory | ~/workspace. |
| Output | The output and the error output of the command, together. |
| Output kept | The live output while it runs, then the last 256 KiB for 90 days. |
| Exit code | The code of the command, in exit_code. The reason is Exited. |
| One wait | At 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
- Access: you.
- References:
POST /v1/sandboxes,POST /v1/sandboxes/{id}/wait,GET /v1/sandboxes/{id}/logs.
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
Create the sandbox with the command. The call returns at once.
sb = kl.sandboxes.create(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])const sb = await kl.sandboxes.create({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });sb, err := kl.Sandboxes.Create(kloudlite.CreateSandbox{Cmd: []string{"python3", "-c", "print(6 * 7)"}, Packages: []string{"python3"}})if err != nil { log.Fatal(err)}sb = kl.sandboxes.create(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])var sb = kl.sandboxes().create(new Kloudlite.CreateSandbox().cmd(List.of("python3", "-c", "print(6 * 7)")).packages("python3"));kloudlite sandboxes create --package python3 -- python3 -c 'print(6 * 7)'Wait for the command to exit. The call returns the exit code.
code = sb.wait()const code = await sb.wait();code, err := sb.Wait(0)if err != nil { log.Fatal(err)}code = sb.waitint code = sb.waitFor(null);kloudlite sandboxes wait sbx-3c4d5e6f7a8b9c0dRead the output.
print(sb.logs())console.log(await sb.logs());out, err := sb.Logs(false)if err != nil { log.Fatal(err)}fmt.Print(out)puts sb.logsSystem.out.print(sb.logs(false));kloudlite sandboxes logs sbx-3c4d5e6f7a8b9c0d
Close-out
- Make sure that the output shows
42and the exit code is0. - 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.
res = kl.sandboxes.run(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])print(res["exit_code"], res["output"])const res = await kl.sandboxes.run({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });console.log(res.exitCode, res.output);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)res = kl.sandboxes.run(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])puts res["exit_code"], res["output"]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);kloudlite sandboxes run --package python3 -- python3 -c 'print(6 * 7)'Troubleshooting
| Symptom | Possible cause | Corrective action |
|---|---|---|
422 for cmd | The command is empty or too long. | Give the program and at most 256 arguments. |
reason is Exited and exit_code is not 0 | The command failed. | Read the output with logs. |
reason is Timeout and exit_code is empty | The 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 expected | The 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 gone | The files of a sandbox are lost when it ends. | Write the result to the output. |