Method: Subprocess.check_output

Defined in:
lib/subprocess.rb

.check_output(cmd, opts = {}) {|process| ... } ⇒ 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)

Parameters:

Yields:

Yield Parameters:

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:



105
106
107
108
109
110
111
# File 'lib/subprocess.rb', line 105

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