Execute shell command and print output continously
svoop opened this issue · comments
Sven Schwyn commented
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")
Sven Schwyn commented
Just figured it out: Use system
instead of backticks – also subshell, but does not capture output and prints it immediately.
Mike Blumtritt commented
If you like to see the executed command use #sh
:
sh 'sleep 10'