Function std.process.pipeProcess
Starts a new process, creating pipes to redirect its standard input, output and/or error streams.
ProcessPipes pipeProcess
(
scope const(char[])[] args,
Redirect redirect = Redirect .all,
const(string[string]) env = cast(const(string[string]))null,
Config config = Config(Flags .none, null, null),
scope const(char)[] workDir = null
) @safe;
ProcessPipes pipeProcess
(
scope const(char)[] program,
Redirect redirect = Redirect .all,
const(string[string]) env = cast(const(string[string]))null,
Config config = Config(Flags .none, null, null),
scope const(char)[] workDir = null
) @safe;
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
,
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.
Parameters
Name | Description |
---|---|
args | An array which contains the program name as the zeroth element
and any command-line arguments in the following elements.
(See spawnProcess for details.) |
program | The program name, without command-line arguments.
(See spawnProcess for details.) |
command | A shell command which is passed verbatim to the command
interpreter. (See spawnShell for details.) |
redirect | Flags that determine which streams are redirected, and
how. See Redirect for an overview of available
flags. |
env | Additional environment variables for the child process.
(See spawnProcess for details.) |
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 | The working directory for the new process. By default the child process inherits the parent's working directory. |
shellPath | The path to the shell to use to run the specified program.
By default this is nativeShell . |
Returns
A ProcessPipes
object which contains 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.
StdioException
on failure to redirect any of the streams.
Example
// 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);
Authors
Lars Tandle Kyllingstad, Steven Schveighoffer, Vladimir Panteleev