5 ways to run commands from Ruby

Every so often I have the need to execute a command line application from a Ruby application/script. And every single time I fail to remember what the different command-executing methods Ruby provides us with do.

This post is primarily a brain dump to aid my failing memory, and it was triggered by an issue with my Redmine Github Hook plugin where STDERR messages were not being logged.

The goal of this exercise is basically to figure out how to run a command and capture all its output - both STDOUT and STDERR - so that the output can be used in the calling script.


The test script I'll be running basically outputs two lines, one on STDOUT, the other on STDERR:

#!/usr/bin/env ruby
puts "out"
STDERR.puts "error"

Kernel#` (backticks)

bq. Returns the standard output of running cmd in a subshell. The built-in syntax %x{...} uses this method. Sets $? to the process status.

>> `./err.rb`
=> "out\n"


bq. Replaces the current process by running the given external command.

>> exec('./err.rb')


bq. Executes cmd in a subshell, returning true if the command was found and ran successfully, false otherwise. An error status is available in $?. The arguments are processed in the same way as for Kernel::exec.

>> system('./err.rb')
=> true


bq. Runs the specified command string as a subprocess; the subprocess's standard input and output will be connected to the returned IO object.

>> output = IO.popen('./err.rb')
=> #<IO:0x1017511b8>
>> err
=> ["out\n"]


bq. Open stdin, stdout, and stderr streams and start external executable.

>> require 'open3'
=> true
>> stdin, stdout, stderr = Open3.popen3('./err.rb')
=> [#<IO:0x101769da8>, #<IO:0x101769d30>, #<IO:0x101769c68>]
>> stdout.readlines
=> ["out\n"]
>> stderr.readlines
=> ["err\n"]

Alternative: Using 'nix redirection

An alternative to Open#popen3 (terrible name) is using standard output redirection:

>> `./err.rb 2>&amp;1`
=> "err\nout\n"

This gives you both STDOUT and STDERR in one big string, which might be perfectly fine if you don't require the granular control that popen3 brings to the table.

I am guessing this method would work for IO#popen as well as for the backticks.