spawnProcessDetached

Spawns a new process, optionally assigning it an arbitrary set of standard input, output, and error streams.

The function returns immediately, leaving the spawned process to execute in parallel with its parent.

The spawned process is detached from its parent, so you should not wait on the returned pid.

  1. void spawnProcessDetached(char[][] args, File stdin, File stdout, File stderr, string[string] env, Config config, char[] workingDirectory, ulong* pid)
    void
    spawnProcessDetached
    (
    in char[][] args
    ,
    File stdin = std.stdio.stdin
    ,
    File stdout = std.stdio.stdout
    ,
    File stderr = std.stdio.stderr
    ,
    const string[string] env = null
    ,
    Config config = Config.none
    ,
    in char[] workingDirectory = null
    ,
    ulong* pid = null
    )
  2. void spawnProcessDetached(char[][] args, string[string] env, Config config, char[] workingDirectory, ulong* pid)

Parameters

args char[][]

An array which contains the program name as the zeroth element and any command-line arguments in the following elements.

stdin File

The standard input stream of the spawned process.

stdout File

The standard output stream of the spawned process.

stderr File

The standard error stream of the spawned process.

env string[string]

Additional environment variables for the child process.

config Config

Flags that control process creation. Same as for spawnProcess.

workingDirectory char[]

The working directory for the new process.

pid ulong*

Pointer to variable that will get pid value in case spawnProcessDetached succeed. Not used if null.

Examples

import std.exception : assertThrown;
version(Posix) {
    try {
        auto devNull = File("/dev/null", "rwb");
        ulong pid;
        spawnProcessDetached(["whoami"], devNull, devNull, devNull, null, Config.none, "./test", &pid);
        assert(pid != 0);

        assertThrown(spawnProcessDetached(["./test/nonexistent"]));
        assertThrown(spawnProcessDetached(["./test/executable.sh"], devNull, devNull, devNull, null, Config.none, "./test/nonexistent"));
        assertThrown(spawnProcessDetached(["./dub.json"]));
        assertThrown(spawnProcessDetached(["./test/notreallyexecutable"]));
    } catch(Exception e) {

    }
}
version(Windows) {
    try {
        ulong pid;
        spawnProcessDetached(["whoami"], std.stdio.stdin, std.stdio.stdout, std.stdio.stderr, null, Config.none, "./test", &pid);

        assertThrown(spawnProcessDetached(["dub.json"]));
    } catch(Exception e) {

    }
}

See Also

Meta