Skip to content

Instantly share code, notes, and snippets.

@isaaguilar
Last active December 7, 2016 06:57
Show Gist options
  • Save isaaguilar/ab4914ef85ce17dd7e2e5f672fb0e0ef to your computer and use it in GitHub Desktop.
Save isaaguilar/ab4914ef85ce17dd7e2e5f672fb0e0ef to your computer and use it in GitHub Desktop.
"tail -f" for ruby
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