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)
  2. void spawnProcessDetached(char[][] args, string[string] env, Config config, char[] workingDirectory, ulong* pid)
    void
    spawnProcessDetached
    (
    in char[][] args
    ,
    const string[string] env
    ,
    Config config = Config.none
    ,
    in char[] workingDirectory = null
    ,
    ulong* pid = null
    )

Parameters

args char[][]

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

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