Process
import "std/sys" (included in import "std") · namespace std.sys
Quick ways to run a shell command line, and information about the current process. The command goes through /bin/sh, so pipes and redirections work, and so does everything else the shell does with the text. Never build the command line from untrusted input; use Command for that, which passes arguments without a shell.
import "std"
fn main() {
let r = Process.run("echo hello | tr a-z A-Z")
print(r.ok())
print(Str.trim(r.output))
print(Process.exec("exit 4"))
print(Str.trim(Process.execOutput("printf 'a\\nb\\n' | wc -l")))
print(Process.pid() > 0)
}true
HELLO
4
2
truerun(cmd)returns aProcessResultwith the exitcodeand the standardoutput(standard error still goes to the terminal).exec(cmd)returns only the exit code, with the output going to your terminal.execOutput(cmd)returns only the output.args()is the command line as anArray<string>;sleepMs,exitandpiddo what their names say.executable()is the running program's absolute path (from/proc/self/exeon Linux, the loader on macOS), whatever directory it was started from: to start more copies of itself as workers with Command, or to find files installed beside it.
Shutting down cleanly
Process.onShutdown(handler) runs handler when the program is asked to stop (SIGTERM from a process manager or kill, SIGINT from Ctrl+C), so a server or worker can finish the request or job in hand, then exit. Process.onSignal(sig, handler) does the same for any signal (SIGHUP to reload, SIGUSR1). Handlers run as ordinary code on a thread of their own: the signal handler itself only writes the signal's number to a pipe. A second SIGINT or SIGTERM while the first is being handled ends the program at once, so a stuck shutdown can still be stopped. (trapSignals and signalReceived remain for programs that would rather ask between units of work.)
import "std"
fn main() {
let jobs = new AtomicInt(0)
Process.onShutdown(fn() {
print("stopping after ${jobs.get()} jobs")
Process.exit(0)
})
for (i in 0..3) { jobs.add(1) }
Process.kill(Process.pid(), Process.SIGTERM())
Thread.sleepMs(2000)
}stopping after 3 jobsAPI
class ProcessResult
The result of Process.run: the exit code and everything the command printed.
Constructors
new ProcessResult(code: int, output: string)
Fields
readonly code: intreadonly output: string
Methods
fn ok() -> bool— Whether the command exited with status 0.
class Process
Static members
static fn exec(cmd: string) -> int— Runs a shell command; returns its exit code (output goes to the terminal).static fn execOutput(cmd: string) -> string— Runs a shell command and captures its standard output.static fn run(cmd: string) -> ProcessResult— Runs a shell command, capturing its output and exit status.static fn pid() -> int— This process's id.static fn executable() -> string— The running program's absolute path ("/home/ada/app/server"): to start more copies of itself (workers), or find files installed beside it.static fn args() -> Array<string>— The program's command-line arguments (without the program name).static fn sleepMs(ms: int)— Pauses formsmilliseconds.static fn exit(code: int)— Ends the program with an exit status.static fn SIGHUP() -> int— The usual signal numbers.static fn SIGINT() -> intstatic fn SIGQUIT() -> intstatic fn SIGUSR1() -> intstatic fn SIGUSR2() -> intstatic fn SIGTERM() -> intstatic fn trapSignals(signals: Array<int>)— Catches these signals instead of letting them end the program: the program asks signalReceived() between units of work (a queue worker finishing its job before stopping on SIGTERM, say).static fn untrapSignal(sig: int)— Lets a signal do what it does by default again.static fn signalReceived() -> int— The last trapped signal received since the previous call (0: none); taking it clears it.static fn onSignal(sig: int, handler: fn())— Runshandler(on a thread of its own, as ordinary code) each timesigarrives, instead of the signal's default action. A second SIGINT or SIGTERM while the first is being handled ends the program at once.static fn onShutdown(handler: fn())— Runshandlerwhen the program is asked to stop (SIGTERM, as from a process manager orkill, or SIGINT, Ctrl+C): finish the work in hand and exit. A second request ends the program at once.static fn kill(pid: int, sig: int) -> bool— Sendssigto processpid(to itself: Process.pid()); whether it went.static fn memoryUsage() -> long— The memory this process holds now (resident set), in bytes.static fn peakMemoryUsage() -> long— The most it has held at once, in bytes.