Module: J1::Utils::Exec2
Instance Method Summary collapse
-
#run(title, *args) ⇒ Object
Runs a program in a sub-shell and return a Process::Status on exit.
Instance Method Details
#run(title, *args) ⇒ Object
Runs a program in a sub-shell and return a Process::Status on exit
title - prepend a title on all messages *args - a list of strings containing the program name and arguments
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/j1/utils/exec2.rb', line 13 def run(title, *args) Open3.popen3(*args) do |stdin, stdout, stderr, status| # manage software interrupt on Ctrl-C trap('INT') { = Time.now.strftime("%Y-%m-%d %H:%M:%S") puts "#{} - #{title}: Received Ctrl-C to stop" [stdin, stdout, stderr].each(&:close) raise SystemExit } # manage messages on stdout stdout_thr = Thread.new do # exit the tread silently Thread.current.report_on_exception = false stdout.each_line do |line| = Time.now.strftime("%Y-%m-%d %H:%M:%S") puts "#{} - #{title}: #{line}" end end # manage messages on stdout stderr_thr = Thread.new do # exit the tread silently Thread.current.report_on_exception = false stderr.each_line do |line| = Time.now.strftime("%Y-%m-%d %H:%M:%S") puts "#{} - " + "\e[31m" + "#{title}: #{line}" + "\e[0m" end end # combine channels stdout, stderr for output [stdout_thr, stderr_thr].each(&:join) # close channels explicitly to exit gracefully [stdin, stdout, stderr].each(&:close) # exit and return Process::Status status.value end end |