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.
| 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 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
- Access: you.
- References:
POST /v1/capsules,POST /v1/capsules/{id}/wait,GET /v1/capsules/{id}/logs.
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
Create the capsule with the command. The call returns at once.
sb = kl.capsules.create(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])const sb = await kl.capsules.create({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });sb, err := kl.Capsules.Create(kloudlite.CreateCapsule{Cmd: []string{"python3", "-c", "print(6 * 7)"}, Packages: []string{"python3"}})if err != nil { log.Fatal(err)}sb = kl.capsules.create(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])var sb = kl.capsules().create(new Kloudlite.CreateCapsule().cmd(List.of("python3", "-c", "print(6 * 7)")).packages("python3"));kode capsules 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);kode capsules wait cap-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));kode capsules logs cap-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 Terminate a capsule.
To run one command in one call, use run. The call ends the capsule for you.
res = kl.capsules.run(cmd=["python3", "-c", "print(6 * 7)"], packages=["python3"])print(res["exit_code"], res["stdout"])const res = await kl.capsules.run({ cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"] });console.log(res.exitCode, res.stdout);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)res = kl.capsules.run(cmd: ["python3", "-c", "print(6 * 7)"], packages: ["python3"])puts res["exit_code"], res["stdout"]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);kode capsules 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 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 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 capsule are lost when it ends. | Write the result to the output. |