2

I'm using popen4 to capture stdout, stderr, and the exit status of a command line. I'm not tied to popen4 as long as I can capture those 3 things above. Currently I've not found a good way to capture command not found errors. I could do a which cmd in a pre-task I suppose, but hoping for something built in.

Below you can run a good task, bad task, and a fake task to see the differences. I'm doing this in a fresh rails new app with the popen4 gem

#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require File.expand_path('../config/application', __FILE__)

require 'open4'

# returns exit status 0, all is good
task :convert_good do
  puts "convert good"
  `wget https://www.google.com/images/srpr/logo3w.png`
  status = Open4.popen4("convert logo3w.png output.jpg") do |pid, stdin,stdout,stderr|
    stdin.close
    puts "stdout:"
    stdout.each_line { |line| puts line }
    puts "stderr: #{stderr.inspect}"
    stderr.each_line { |line| puts line }
  end
  puts "status: #{status.inspect}"
  puts "exit:   #{status.exitstatus}"
end

# returns exit status 1, we messed up our command
task :convert_bad do
  puts "convert bad"
  status = Open4.popen4("convert logo3w-asdfasdf.png output.jpg") do |pid, stdin,stdout,stderr|
    stdin.close
    puts "stdout:"
    stdout.each_line { |line| puts line }
    puts "stderr: #{stderr.inspect}"
    stderr.each_line { |line| puts line }
  end
  puts "status: #{status.inspect}"
  puts "exit:   #{status.exitstatus}"
end

# I want this to return exit code 127 for command not found
task :convert_none do
  puts "convert bad"
  status = Open4.popen4("convert_not_installed") do |pid, stdin,stdout,stderr|
    stdin.close
    puts "stdout:"
    stdout.each_line { |line| puts line }
    puts "stderr: #{stderr.inspect}"
    #it doesnt like stderr in this case
    #stderr.each_line { |line| puts line }
  end
  puts "status: #{status.inspect}"
  puts "exit:   #{status.exitstatus}"
end

Here are the 3 local outputs

# good
stdout:
stderr: #<IO:fd 11>
status: #<Process::Status: pid 17520 exit 0>
exit:   0

# bad arguments
convert bad
stdout:
stderr: #<IO:fd 11>
convert: unable to open image `logo3w-asdfasdf.png': No such file or directory @ blob.c/OpenBlob/2480.
convert: unable to open file `logo3w-asdfasdf.png' @ png.c/ReadPNGImage/2889.
convert: missing an image filename `output.jpg' @ convert.c/ConvertImageCommand/2800.
status: #<Process::Status: pid 17568 exit 1>
exit:   1

# fake command not found, but returns exit 1 and stderr has no lines
convert bad
stdout:
stderr: #<IO:fd 11>
status: #<Process::Status: pid 17612 exit 1>
exit:   1
chrisan
  • 4,152
  • 4
  • 28
  • 32

1 Answers1

5

A couple of points first.

  1. You're not actually using the popen4 gem - which is a wrapper around the open4 gem (if you're running on a Unix system, at least) - you're using the open4 gem directly. If you wanted to use popen4, you'd call it like this:

    status = POpen4.popen4('cmd') do |stdout, stderr, stdin, pid|
      # ...
    end
    
  2. The popen4 method ultimately executes the specified command via the Kernel#exec method, and the behaviour of that depends on whether it determines the given command should be run in a shell or not. (You can see http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-exec, but it's not terribly helpful. The source code is a better bet.)

For example:

>  fork { exec "wibble" }
 => 1570 
> (irb):56:in `exec': No such file or directory - wibble (Errno::ENOENT)
    from (irb):56:in `irb_binding'
    from (irb):56:in `fork'
    from (irb):56:in `irb_binding'
    from /Users/evilrich/.rvm/rubies/ree-1.8.7-2011.03/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'
    from :0

Here, exec was trying to execute the non-existent command 'wibble' directly - hence the exception.

> fork { exec "wibble &2>1" }
 => 1572 
> sh: wibble: command not found

Here, exec saw I was using redirection and so executed my command in a shell. The difference? I get an error on STDERR and no exception. You can also force a shell to be used by specifying that in the command to be executed:

> fork { exec "sh -c 'wibble -abc -def'" }

Anyway, understanding the behaviour of Kernel#exec may help in getting the popen4 method to behave the way you want it to.

To answer your question, if I use the popen4 gem and construct the command in such a way that (by exec's rules) it'll get run in a shell or if I use "sh -c ..." in the command myself, then I get the kind of behaviour I think you are looking for:

> status = POpen4.popen4("sh -c 'wibble -abc -def'") {|stdout, stderr, stdin, pid| puts "Pid: #{pid}"}
Pid: 1663
 => #<Process::Status: pid=1663,exited(127)> 
> puts status.exitstatus
127

Update

Interesting. Open4.popen will also return a 127 exit status if you read from stderr. So, there's no need to use the popen gem.

> status = Open4.popen4("sh -c 'wibble -abc -def'") {|pid, stdin, stdout, stderr| stderr.read }
 => #<Process::Status: pid 1704 exit 127> 
Rich Drummond
  • 3,439
  • 1
  • 15
  • 16
  • Hah nice catch on Open4 vs POpen4. So that did indeed catch exit 127, thank you. However now in my `convert_bad` example, instead of returning the error about unable to open the non-existent image, it just prints the help screen for ImageMagick as if you had just ran `convert` with no arguments – chrisan Feb 17 '13 at 11:36