Home Explore Blog CI



neovim

18th chunk of `runtime/doc/luvref.txt`
506297212ee66ec709808e3950fa3fe5ddc860eab4b8d8060000000100000fa0
 there is no
                guarantee that libuv can discover all file descriptors that
                were inherited. In general it does a better job on Windows
                than it does on Unix.

uv.spawn({path}, {options}, {on_exit})                              *uv.spawn()*

                Parameters:
                - `path`: `string`
                - `options`: `table` (see below)
                - `on_exit`: `callable`
                  - `code`: `integer`
                  - `signal`: `integer`

                Initializes the process handle and starts the process. If the
                process is successfully spawned, this function will return the
                handle and pid of the child process.

                Possible reasons for failing to spawn would include (but not
                be limited to) the file to execute not existing, not having
                permissions to use the setuid or setgid specified, or not
                having enough memory to allocate for the new process.

                    >lua
                    local stdin = uv.new_pipe()
                    local stdout = uv.new_pipe()
                    local stderr = uv.new_pipe()

                    print("stdin", stdin)
                    print("stdout", stdout)
                    print("stderr", stderr)

                    local handle, pid = uv.spawn("cat", {
                      stdio = {stdin, stdout, stderr}
                    }, function(code, signal) -- on exit
                      print("exit code", code)
                      print("exit signal", signal)
                    end)

                    print("process opened", handle, pid)

                    uv.read_start(stdout, function(err, data)
                      assert(not err, err)
                      if data then
                        print("stdout chunk", stdout, data)
                      else
                        print("stdout end", stdout)
                      end
                    end)

                    uv.read_start(stderr, function(err, data)
                      assert(not err, err)
                      if data then
                        print("stderr chunk", stderr, data)
                      else
                        print("stderr end", stderr)
                      end
                    end)

                    uv.write(stdin, "Hello World")

                    uv.shutdown(stdin, function()
                      print("stdin shutdown", stdin)
                      uv.close(handle, function()
                        print("process closed", handle, pid)
                      end)
                    end)
<
                                                              *uv.spawn-options*
                The `options` table accepts the following fields:

                  - `options.args` - Command line arguments as a list of
                    strings. The first string should not be the path to the
                    program, since that is already provided via `path`. On
                    Windows, this uses CreateProcess which concatenates the
                    arguments into a string. This can cause some strange
                    errors (see `options.verbatim` below for Windows).
                  - `options.stdio` - Set the file descriptors that will be
                    made available to the child process. The convention is
                    that the first entries are stdin, stdout, and stderr.
                    (Note: On Windows, file descriptors after the third are
                    available to the child process only if the child processes
                    uses the MSVCRT runtime.)
                  - `options.env` - Set environment variables for the new
                    process.
                  - `options.cwd` - Set the current working directory for the
                    sub-process.
                  - `options.uid` - Set the child process' user id.
                  - `options.gid` - Set

Title: Libuv: uv.spawn() and Options
Summary
This section provides details on the `uv.spawn()` function, used to initialize and start a new process with specified parameters such as the path to the executable, a table of options, and an exit callback function. A Lua code example demonstrates how to use `uv.spawn()` to execute the 'cat' command, establish communication channels via pipes for stdin, stdout, and stderr, and manage process exit and shutdown. It also outlines the available options for the `options` table in `uv.spawn()`, including `args`, `stdio`, `env`, `cwd`, `uid`, and `gid`, offering control over the child process's environment.