Module Spawn

Mini spawn library

Note: on Unix, spawn uses vfork by default. It has been tested, but if you believe this is causing a problem in your application, you can change this default at runtime by setting the environment variable SPAWN_USE_FORK.

module Working_dir : sig ... end
module Unix_backend : sig ... end
val spawn : ?env:string list ‑> ?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.


env must be a list of strings of the form "KEY=VALUE". It 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.

val safe_pipe : unit ‑> Unix.file_descr * Unix.file_descr