open4 stdin.close required?

B

Brendan G.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

[ANN] open4-0.9.2 0
[ANN] open4-0.9.0 0
[ANN] open4-0.8.0 2
[ANN] open4-0.5.1 0
[ANN] open4-0.2.0 0
[ANN] open4-0.6.0 0
[ANN] open4-0.5.0 0
[ANN] open4-0.7.0 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top