This method doesn’t wait for end of the command. The parent process
should use Process.wait to collect
the termination status of its child or use Process.detach to register
disinterest in their status; otherwise, the operating system may accumulate zombie processes.

If a hash is given as env, the environment is updated by
env before exec(2) in the child
process. If a pair in env has nil as the value, the variable is
deleted.

# set FOO as BAR and unset BAZ.pid=spawn({"FOO"=>"BAR","BAZ"=>nil},command)

If a hash is given as options, it specifies process group, create
new process group, resource limit, current directory, umask and redirects
for the child process. Also, it can be specified to clear environment
variables.

The :unsetenv_others key in options specifies to clear environment
variables, other than specified by env.

pid=spawn(command,:unsetenv_others=>true)# no environment variablepid=spawn({"FOO"=>"BAR"},command,:unsetenv_others=>true)# FOO only

The :pgroup key in options specifies a process group. The
corresponding value should be true, zero or positive integer. true and zero
means the process should be a process leader of a new process group. Other
values specifies a process group to be belongs.

pid=spawn(command,:pgroup=>true)# process leaderpid=spawn(command,:pgroup=>10)# belongs to the process group 10

The :new_pgroup key in options specifies to pass
CREATE_NEW_PROCESS_GROUP flag to CreateProcessW() that is Windows
API. This option is only for Windows.
true means the new process is the root process of the new process group.
The new process has CTRL+C disabled. This flag is necessary for
Process.kill(:SIGINT, pid) on the subprocess. :new_pgroup is false by
default.

pid=spawn(command,:new_pgroup=>true)# new process grouppid=spawn(command,:new_pgroup=>false)# same process group

The :rlimit_foo key specifies a resource limit. foo should be one
of resource types such as core. The corresponding value should be an
integer or an array which have one or two integers: same as cur_limit and
max_limit arguments for Process.setrlimit.

The array specifies a filename, flags and permission. The flags can be a
string or an integer. If the flags is omitted or nil, File::RDONLY is
assumed. The permission should be an integer. If the permission is omitted
or nil, 0644 is assumed.

If an array of IOs and integers are specified as a hash key, all the
elements are redirected.

# stdout and stderr is redirected to log file.# The file "log" is opened just once.pid=spawn(command,[:out,:err]=>["log","w"])

Another way to merge multiple file descriptors is [:child, fd]. [:child,
fd] means the file descriptor in the child process. This is different from
fd. For example, :err=>:out means redirecting child stderr to parent
stdout. But :err=>[:child, :out] means redirecting child stderr to child
stdout. They differs if stdout is redirected in the child process as
follows.

# stdout and stderr is redirected to log file.# The file "log" is opened just once.pid=spawn(command,:out=>["log","w"],:err=>[:child,:out])

[:child, :out] can be used to merge stderr into stdout in IO.popen. In this
case, IO.popen redirects stdout to a
pipe in the child process and [:child, :out] refers the redirected stdout.

spawn closes all non-standard unspecified
descriptors by default. The “standard” descriptors are 0, 1 and 2. This
behavior is specified by :close_others option. :close_others doesn’t
affect the standard descriptors which are closed only if :close is
specified explicitly.