Module Spawn

Mini spawn library

module Working_dir : sig ... end
module Unix_backend : sig ... end
module Env : sig ... end
val spawn : ?⁠env:Env.t -> ?⁠cwd:Working_dir.t -> prog:string -> argv:string list -> ?⁠stdin:Unix.file_descr -> ?⁠stdout:Unix.file_descr -> ?⁠stderr:Unix.file_descr -> ?⁠unix_backend:Unix_backend.t -> unit -> int

Spawn a sub-command and return its PID. This function is low-level and should be used to build higher-level APIs.

In case of errors, it raises Unix.Unix_error.


prog is not searched in PATH. It is up to the caller to do the path resolution before calling this function. Note that there is no special treatment of executable text files without a proper #!. The execvp function from the C library calls /bin/sh in this case to imitate the behaviors of a shell but this function doesn't.

Note that when prog is a relative filename, it is interpreted as a path relative to the working directory specified by the cwd argument. On Windows, this differs from what the underlying CreateProcess function does.

Command line arguments

argv is the full command line. The first element should be the program name and subsequent elements the command line arguments. Note that the head of argv doesn't necessarily have to be equal to prog. For instance it might be foo while prog might be /usr/bin/foo.


env represents the environment in which the sub-process is executed. If not specified, the environment from the process calling this function is used.

Working directory

cwd describes what the current working directory of the sub-process should be. It defaults to Inherit. It is an error to pass Fd _ on Windows.

Standard input/outputs

stdin, stdout and stderr are the file descriptors used as standard input, output and error output of the sub-process. When not specified, they default to the ones from the calling process.


On Unix, the sub-process will have all its signals unblocked.


unix_backend describes what backend to use on Unix. If set to Default, vfork is used unless the environment variable SPAWN_USE_FORK is set. On Windows, CreateProcess is used.