Skip to content
class method

PTY.spawn

(*::String command) -> [ ::IO, ::IO, ::Integer ]
(*::String command) { ([ ::IO, ::IO, ::Integer ]) -> void } -> void
PTY.spawn([env,] command_line) { |r, w, pid| ... }
PTY.spawn([env,] command_line) => [r, w, pid]
PTY.spawn([env,] command, arguments, ...) { |r, w, pid| ... }
PTY.spawn([env,] command, arguments, ...) => [r, w, pid]

Spawns the specified command on a newly allocated pty. You can also use the alias ::getpty.

The command’s controlling tty is set to the slave device of the pty and its standard input/output/error is redirected to the slave device.

env is an optional hash that provides additional environment variables to the spawned pty.

Example
# sets FOO to "bar"
PTY.spawn({"FOO"=>"bar"}, "printenv", "FOO") do |r, w, pid|
  p r.read #=> "bar\r\n"
ensure
  r.close; w.close; Process.wait(pid)
end
# unsets FOO
PTY.spawn({"FOO"=>nil}, "printenv", "FOO") do |r, w, pid|
  p r.read #=> ""
ensure
  r.close; w.close; Process.wait(pid)
end

command and command_line are the full commands to run, given a String. Any additional arguments will be passed to the command.

Return values

In the non-block form this returns an array of size three, [r, w, pid].

In the block form these same values will be yielded to the block:

r

A readable IO that contains the command’s standard output and standard error

w

A writable IO that is the command’s standard input

pid

The process identifier for the command.

Clean up

This method does not clean up like closing IOs or waiting for child process, except that the process is detached in the block form to prevent it from becoming a zombie (see Process.detach). Any other cleanup is the responsibility of the caller. If waiting for pid, be sure to close both r and w before doing so; doing it in the reverse order may cause deadlock on some OSes.

See also .open .getpty also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0