How to get exit status for a command called using IO.popen?

I use IO.popen to execute a command and capture output as follows:

process = IO.popen("sudo -u service_user -i start_service.sh") do |io| while line = io.gets line.chomp! process_log_line(line) end end 

How can I capture the exit status * start_service.sh *?

+8
ruby
source share
1 answer

Can you capture the exit status of a command called via IO.open () by referencing $? until you shut the phone at the end of your unit.

In the above example, you should:

  process = IO.popen("sudo -u service_user -i start_service.sh") do |io| while line = io.gets line.chomp! process_log_line(line) end io.close do_more_stuff if $?.to_i == 0 end 

For more information, see Ruby Core library entry for IO.popen .

+11
source share

All Articles