std.process
std.process: child processes. `spawn` starts a program (found through `PATH` when its name has no `/`) with its standard streams inherited or piped; `Child.wait` gives the exit status, `Child.kill` sends SIGTERM. `Child.killHard` sends SIGKILL. The child runs in a copy of this process's environment and working directory unless `spawnWith` gets `SpawnOptions`: `env` (entries `NAME=value`) replaces the whole environment, `cwd` is the directory the child starts in (a missing one is `NotFound`). A pipe is a `std.fs.File`: read the child's output with `readAll` or `read`, write its input with `write`.
SIGPIPE: a write to the stdin of a child that has exited must be a `WriteError`, not the end of this process. A Linux pipe has no MSG_NOSIGNAL (std.net uses it per socket there, SO_NOSIGPIPE on macOS), so the runtime's `spawn` helper sets SIGPIPE to ignored once for the whole process (the write then fails with EPIPE), and restores the default action in the child before `exec`, because an ignored signal would be inherited by the program. The same on macOS and Linux.
SpawnError
pub error SpawnError { EmptyArgv, NotFound, AccessDenied, OutOfMemory, SpawnFailed }
WaitError
pub error WaitError { WaitFailed }
KillError
pub error KillError { NoSuchProcess, KillFailed }
Stdio
pub enum Stdio {
Inherit
Pipe
}
What a child's standard stream is connected to.
Inherit-
The same as this process's stream.
Pipe-
A pipe to this process: `Child.stdin` (written by the parent), `Child.stdout` or `Child.stderr` (read by the parent).
Status
pub struct Status {
code: i32
signal: i32
pub fn success(self: Self) bool
}
How a child ended: it exited with `code` (and `signal` is 0), or a signal ended it (`signal` is the number, `code` is 0).
pub fn success(self: Self) bool-
Did the child exit with status 0?
SpawnOptions
pub struct SpawnOptions {
env: ?[][]u8 = null
cwd: ?[]u8 = null
}
Extra choices for `spawnWith`. A null field keeps the default.
env: ?[][]u8 = null-
The child's whole environment, entries `NAME=value`.
cwd: ?[]u8 = null-
The child's working directory.
Child
pub struct Child {
pid: i32
stdin: ?File
stdout: ?File
stderr: ?File
waited: bool = false
pub fn closeStdin(self: *mut Self)
pub fn wait(self: *mut Self) WaitError!Status
pub fn kill(self: *mut Self) KillError!void
pub fn killHard(self: *mut Self) KillError!void
}
A started child process. The pipes that `spawn` was asked for are set (`stdin`, `stdout`, `stderr`); the others are null. Close each pipe you use (`close`); `wait` and `closeStdin` close `stdin`.
pub fn closeStdin(self: *mut Self)-
Closes the pipe to the child's stdin, so that the child reads the end of its input. Does nothing when stdin is not piped or is closed.
pub fn wait(self: *mut Self) WaitError!Status-
Closes stdin, then waits until the child has ended and gives how. Call it once; it gives `WaitFailed` after that.
pub fn kill(self: *mut Self) KillError!void-
Asks the child to end (SIGTERM). It gives `NoSuchProcess` after `wait`.
pub fn killHard(self: *mut Self) KillError!void-
Ends the child at once (SIGKILL, which it cannot catch). It gives `NoSuchProcess` after `wait`.
spawn
pub fn spawn(alloc: Allocator, argv: [][]u8, stdin: Stdio, stdout: Stdio, stderr: Stdio) SpawnError!Child
Starts the program `argv[0]` with the arguments `argv` (the first is the program's own name). The three streams are `Inherit` (this process's) or `Pipe` (see `Child`). `alloc` is used only during the call. An argument must not contain a NUL byte. Output of `@print` is flushed first, so the child's output follows it.
spawnWith
pub fn spawnWith(alloc: Allocator, argv: [][]u8, stdin: Stdio, stdout: Stdio, stderr: Stdio, opts: SpawnOptions) SpawnError!Child
`spawn` with a custom environment and working directory (`opts`).