Grav v1.14.0

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.

grav
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)
}
Output
true
HELLO
4
2
true
  • run(cmd) returns a ProcessResult with the exit code and the standard output (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 an Array<string>; sleepMs, exit and pid do what their names say.
  • executable() is the running program's absolute path (from /proc/self/exe on 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.)

grav
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)
}
Output
stopping after 3 jobs

API

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: int
  • readonly 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 for ms milliseconds.
  • static fn exit(code: int) — Ends the program with an exit status.
  • static fn SIGHUP() -> int — The usual signal numbers.
  • static fn SIGINT() -> int
  • static fn SIGQUIT() -> int
  • static fn SIGUSR1() -> int
  • static fn SIGUSR2() -> int
  • static fn SIGTERM() -> int
  • static 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()) — Runs handler (on a thread of its own, as ordinary code) each time sig arrives, 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()) — Runs handler when the program is asked to stop (SIGTERM, as from a process manager or kill, 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 — Sends sig to process pid (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.

Edit this page on GitHub