Method: Subprocess.check_output

Defined in:
lib/subprocess.rb

.check_output(cmd, opts = {}, &blk) ⇒ String

Like check_call, but return the contents of stdout, much like ‘Kernel#system`.

Examples:

Get the system load

system_load = Subprocess.check_output(['uptime']).split(' ').last(3)

Returns:

  • (String)

    The contents of stdout

Raises:

  • (NonZeroExit)

    if the process returned a non-zero exit status (i.e., was terminated with an error or was killed by a signal)

See Also:

  • {Process{Process#initialize}


84
85
86
87
88
89
90
# File 'lib/subprocess.rb', line 84

def self.check_output(cmd, opts={}, &blk)
  opts[:stdout] = PIPE
  child = Process.new(cmd, opts, &blk)
  output, _ = child.communicate()
  raise NonZeroExit.new(cmd, child.status) unless child.wait.success?
  output
end