Skip to content

Run another program

Starting programs needs the os.processes() permission. Narrow it to the programs you mean to run, so a mistake elsewhere cannot start anything else. Arguments are passed as a list, never through a shell, so text with spaces or quotes in it cannot turn into extra commands.

import os
let procs = os.processes().only(&["echo", "sh"])
let out = try procs.run_output(&["echo", "hello; rm -rf /"])
print(f"status {out.status}")
print(out.out.unchecked().trim())
let status = try procs.run(&["sh", "-c", "exit 3"])
print(f"exit code {status}")
let refused = procs.run(&["curl", "example.com"]) catch -1
print(f"curl: {refused}")
status 0
hello; rm -rf /
exit code 3
curl: -1

The output is Untrusted<Str>: it came from outside the program. Here unchecked() says plainly that it is only printed.

A long-running program is started with spawn, which gives a Process. A Process must be finished, with wait() or kill(), on every path: forgetting it is a compile error, not a leaked process.