Open4 stdin.close required?

Hi,

This is probably a stupid question, but I’m using open4 and I am
wondering if I’m using it ‘correctly’. Anyway the following is from the
examples for open 4. Notice that in this case a stdin.close call is made
which makes sense to me in this case:

require "open4"

pid, stdin, stdout, stderr = Open4::popen4 "sh"

stdin.puts "echo 42.out"
stdin.puts "echo 42.err 1>&2"
stdin.close

ignored, status = Process::waitpid2 pid

puts "pid        : #{ pid }"
puts "stdout     : #{ stdout.read.strip }"
puts "stderr     : #{ stderr.read.strip }"
puts "status     : #{ status.inspect }"
puts "exitstatus : #{ status.exitstatus }"

But what if I’m invoking a command like this:

require “open4”

pid, stdin, stdout, stderr = Open4::popen4 "cd /some/random/dir &&

run_a_script"
stdin.close

ignored, status = Process::waitpid2 pid

puts "pid        : #{ pid }"
puts "stdout     : #{ stdout.read.strip }"
puts "stderr     : #{ stderr.read.strip }"
puts "status     : #{ status.inspect }"
puts "exitstatus : #{ status.exitstatus }"

Do I still need the stdin.close? Also, is it advisable to always open a
separate shell and invoke the command from stdin? Something like:

require “open4”

pid, stdin, stdout, stderr = Open4::popen4 "sh"
stdin.puts "cd /some/random/dir && run_a_script"
stdin.close

ignored, status = Process::waitpid2 pid

puts "pid        : #{ pid }"
puts "stdout     : #{ stdout.read.strip }"
puts "stderr     : #{ stderr.read.strip }"
puts "status     : #{ status.inspect }"
puts "exitstatus : #{ status.exitstatus }"

Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs