pipeShell

Starts a new process, creating pipes to redirect its standard input, output and/or error streams.

pipeProcess and pipeShell are convenient wrappers around spawnProcess and spawnShell, respectively, and automate the task of redirecting one or more of the child process' standard streams through pipes. Like the functions they wrap, these functions return immediately, leaving the child process to execute in parallel with the invoking process. It is recommended to always call wait on the returned ProcessPipes.pid, as detailed in the documentation for wait.

The args/program/command, env and config parameters are forwarded straight to the underlying spawn functions, and we refer to their documentation for details.

@safe
pipeShell
(
scope const(char)[] command
,,
const string[string] env = null
,,
scope const(char)[] workDir = null
,)

Parameters

command const(char)[]

A shell command which is passed verbatim to the command interpreter. (See spawnShell for details.)

redirect Redirect

Flags that determine which streams are redirected, and how. See Redirect for an overview of available flags.

env string[string]

Additional environment variables for the child process. (See spawnProcess for details.)

config Config

Flags that control process creation. See Config for an overview of available flags, and note that the retainStd... flags have no effect in this function.

workDir const(char)[]

The working directory for the new process. By default the child process inherits the parent's working directory.

shellPath string

The path to the shell to use to run the specified program. By default this is nativeShell.

Return Value

A ProcessPipes object which contains std.stdio.File handles that communicate with the redirected streams of the child process, along with a Pid object that corresponds to the spawned process.

Throws

ProcessException on failure to start the process.
std.stdio.StdioException on failure to redirect any of the streams.

Examples

// my_application writes to stdout and might write to stderr
auto pipes = pipeProcess("my_application", Redirect.stdout | Redirect.stderr);
scope(exit) wait(pipes.pid);

// Store lines of output.
string[] output;
foreach (line; pipes.stdout.byLine) output ~= line.idup;

// Store lines of errors.
string[] errors;
foreach (line; pipes.stderr.byLine) errors ~= line.idup;


// sendmail expects to read from stdin
pipes = pipeProcess(["/usr/bin/sendmail", "-t"], Redirect.stdin);
pipes.stdin.writeln("To: you");
pipes.stdin.writeln("From: me");
pipes.stdin.writeln("Subject: dlang");
pipes.stdin.writeln("");
pipes.stdin.writeln(message);

// a single period tells sendmail we are finished
pipes.stdin.writeln(".");

// but at this point sendmail might not see it, we need to flush
pipes.stdin.flush();

// sendmail happens to exit on ".", but some you have to close the file:
pipes.stdin.close();

// otherwise this wait will wait forever
wait(pipes.pid);

Meta