ruby / rake

A make-like build utility for Ruby.

Home Page:https://ruby.github.io/rake

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Execute shell command and print output continously

svoop opened this issue · comments

Sometimes, it's unavoidable to have a Rake task execute shell commands. Doing it with backticks works, but the output of a long running shell command is only printed once the command finishes:

puts `do_something_that_takes_a_while`

One approach to work around this is the use of popen:

IO.popen("do_something_that_takes_a_while") do |io| 
    while line = io.gets  { puts line }
end

Is there a more elegant way to achieve this?

And if not, would a PR to Rake which wraps the popen workaround stand the chance of being accepted?

Something along the lines of (better ideas welcome):

Rake::System("do_something_that_takes_a_while")

Just figured it out: Use system instead of backticks – also subshell, but does not capture output and prints it immediately.

If you like to see the executed command use #sh:

  sh 'sleep 10'