execute

Executes the given program or shell command and returns its exit code and output.

execute and executeShell start a new process using spawnProcess and spawnShell, respectively, and wait for the process to complete before returning. The functions capture what the child process prints to both its standard output and standard error streams, and return this together with its exit code.

auto dmd = execute(["dmd", "myapp.d"]);
if (dmd.status != 0) writeln("Compilation failed:\n", dmd.output);

auto ls = executeShell("ls -l");
if (ls.status != 0) writeln("Failed to retrieve file listing");
else writeln(ls.output);

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

  1. auto execute(const(char[])[] args, string[string] env, Config config, size_t maxOutput, const(char)[] workDir)
    @safe
    execute
    (
    scope const(char[])[] args
    ,
    const string[string] env = null
    ,,
    size_t maxOutput = size_t.max
    ,
    scope const(char)[] workDir = null
    )
  2. auto execute(const(char)[] program, string[string] env, Config config, size_t maxOutput, const(char)[] workDir)

Parameters

args const(char[])[]

An array which contains the program name as the zeroth element and any command-line arguments in the following elements. (See spawnProcess for details.)

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.

maxOutput size_t

The maximum number of bytes of output that should be captured.

workDir const(char)[]

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

Return Value

Type: auto

An std.typecons.Tuple!(int, "status", string, "output").

POSIX specific

If the process is terminated by a signal, the status field of the return value will contain a negative number whose absolute value is the signal number. (See wait for details.)

Throws

ProcessException on failure to start the process.
std.stdio.StdioException on failure to capture output.

Meta