Skip to content
DocsAll docs
Console

Commands

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

Last updated

On this page

Overview

A command is the main process of a capsule. You give it at creation as cmd: the program, then its arguments. The capsule 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 capsule waits only for the command. When the command exits, the other processes end with the capsule.

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

A command can use the name of a service when the capsule is created in a desk with an environment. See Capsules.

Tasks

Run a command

Reason for the task

Run a command in a new capsule and read the output and the exit code.

Job set-up

WARNING

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

Procedure

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

    Python
    sb = kl.capsules.create(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])
    TypeScript
    const sb = await kl.capsules.create({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });
    Go
    sb, err := kl.Capsules.Create(kloudlite.CreateCapsule{Cmd: []string{"python3", "-c", "print(6 * 7)"}, Packages: []string{"python3"}})if err != nil {    log.Fatal(err)}
    Ruby
    sb = kl.capsules.create(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])
    Java
    var sb = kl.capsules().create(new Kloudlite.CreateCapsule().cmd(List.of("python3", "-c", "print(6 * 7)")).packages("python3"));
    CLI
    kode capsules 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
    kode capsules wait cap-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
    kode capsules logs cap-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 Terminate a capsule.

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

Python
res = kl.capsules.run(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])print(res["exit_code"], res["stdout"])
TypeScript
const res = await kl.capsules.run({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });console.log(res.exitCode, res.stdout);
Go
res, err := kl.Capsules.Run(kloudlite.RunCapsule{Capsule: kloudlite.CreateCapsule{Cmd: []string{"python3", "-c", "print(6 * 7)"}, Packages: []string{"python3"}}})if err != nil {	log.Fatal(err)}fmt.Println(res.ExitCode, res.Stdout)
Ruby
res = kl.capsules.run(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])puts res["exit_code"], res["stdout"]
Java
var res = kl.capsules().run(new Kloudlite.RunCapsule().cmd(List.of("python3", "-c", "print(6 * 7)")).packages("python3"));System.out.println(res.exitCode + " " + res.stdout);
CLI
kode capsules 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 capsule with a larger timeout_s.
409 with has no output yet (starting)The capsule has not started.Wait, then read the output again. An empty answer after the start means no output yet.
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 capsule are lost when it ends.Write the result to the output.