Last active
December 7, 2016 06:57
-
-
Save isaaguilar/ab4914ef85ce17dd7e2e5f672fb0e0ef to your computer and use it in GitHub Desktop.
"tail -f" for ruby
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
require 'open3' | |
def tail_and_quit(filename, regex_to_match) | |
cmd = "tail -n0 -f #{filename}" # bash tail -f command | |
Open3.popen2e('bash', '-c', cmd) do |i,oe,t| # input, output/error, process | |
oe.each do |line| | |
puts line | |
break if line.match(/#{regex_to_match}/) | |
end | |
Process.kill('INT', t.pid) # kill the tail process after matching string | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment