tags:

views:

81

answers:

3

In Ruby, what is the simplest way to execute a local Linux command stored in a string while catching any potential exceptions that are thrown and logging the output of the Linux command and any thrown errors to a common log file?

+1  A: 

look at IO.popen

ennuikiller
A: 

Here is the code I use to see whether a process is active:

systemOutput=`ps -A | grep #{process_to_look_for}`
if systemOutput.include? process_to_look_for
  puts "#{process_to_look_for} is already running"
  exit
end
Jasim
+3  A: 

I was faced same question before, and this resource answered all my need.

if you don't want to separate error from normal output just use popen

output = IO.popen("other_program", "w+") do |pipe|
  pipe.puts "here, have some input"
  pipe.close_write
  pipe.read
end

but if you do want to, use popen3

Open3.popen3('nroff -man') { |stdin, stdout, stderr| ... }
Jirapong