Module std::process

Process execution.

Run external commands and manage child processes.

process.Child is an owned handle. Dropping a Child reaps the OS process handle: if the child has already exited, drop performs the final wait; if it is still running, drop kills it and waits for it to exit.

Examples

import std.process;

fn main() {
    let shell_output = process.run("echo hello").expect("run succeeds");
    println(shell_output.stdout);

    var args: Vec<string> = Vec.new();
    args.push("<%s>|<%s>");
    args.push("hello world");
    args.push("hew");
    let output = process.run_argv("printf", args).expect("run_argv succeeds");
    println(output.stdout);

    let child = match process.start("sleep 10") {
        .Ok(child) => child,
        .Err(error) => panic(to_string(error)),
    };
    println(child.wait());
}

Contents

Functions

Function run

pub fn run(command: string) -> Result<CommandOutput, ProcessError>

Run a shell command synchronously and capture its exit code and output.

Function run_argv

pub fn run_argv(command: string, args: Vec<string>) -> Result<CommandOutput, ProcessError>

Run a command without the shell using a Vec<string> argv surface.

Function run_args

pub fn run_args(command: string, args: string, arg_count: i64) -> Result<CommandOutput, ProcessError>

Legacy compatibility wrapper for a space-delimited argument string.

This packed-string surface cannot represent empty arguments, quoted segments, or arguments containing spaces. Prefer run_argv(command, Vec<string>).

Function start

pub fn start(command: string) -> Result<Child, ProcessError>

Start a child process through the system shell without blocking.

Returns Err(ProcessError.LaunchFailed) when the child could not be launched, rather than a handle that is not backed by a running process.

Function start_argv

pub fn start_argv(command: string, args: Vec<string>) -> Result<Child, ProcessError>

Start a child process without the shell using a Vec<string> argv surface.

Returns Err(ProcessError.LaunchFailed) when the executable cannot be launched โ€” a missing command is reported here rather than surfacing as a handle that never referred to a process.

Types

Struct ProcessResultHandle

A handle to a completed command invocation inside the runtime.

Struct Child

A handle to a running child process.

Created by process.start(cmd). Use wait() to block until the process exits, or kill() to terminate it. Dropping the handle reaps it automatically; a still-running child is killed and waited on at scope exit.

Fields

handle: ChildHandle

Struct CommandOutput

Captured output from a completed command.

Fields

exit_code: i64
stdout: string
stderr: string

Enum ProcessError

Structured execution errors for process launch and argument validation.

Variants

InvalidArguments(string)
LaunchFailed(string)

Traits

Trait ChildMethods

Methods available on a Child process.

Methods

fn wait(self: Self) -> i64

Block until the child process exits and return its exit code.

Returns 0 for success, non-zero for failure.

fn kill(self: Self) -> i64

Terminate the child process.

Returns 0 on success, non-zero on error.