Last active
June 2, 2023 14:37
-
-
Save dkandalov/0af9d05339e6b29a8213 to your computer and use it in GitHub Desktop.
Ruby method to execute and continuously print output of long-running shell command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/ruby | |
| require "open4" | |
| def shell_exec(command) | |
| puts("> " + command + "\n") | |
| pid, stdin, stdout, stderr = Open4::popen4(command) | |
| while Process::waitpid(pid, Process::WNOHANG).nil? do | |
| stdout.each_line { |line| puts line } | |
| stderr.each_line { |line| puts line } | |
| sleep(1) | |
| end | |
| stdout.each_line { |line| puts line } | |
| stderr.each_line { |line| puts line } | |
| end | |
| # shell_exec("ls -l && sleep 5 && ls -l && sleep 2") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for that, here is a similar one with Open3: