Using the following -f on tail option, a executed command will not immediately end.
-f, --follow[={name|descriptor}] output appended data as the file grows;
The idea of using backticks (or %x shortcut) as opposed to using system('...') is that these statements return the output from executed commands. This way you can save the result in a variable:
dir_content = `ls`
tail -f spawns another process, this process continues to write to standard output without interruption. Therefore, your expression never ends. Without completion of the instruction, the value cannot be returned. If you want to watch and display a growing file, see Solution:
View / read a growing log file .
Alternatively, you can run the command through system as follows:
system('tail -f filename')
What is the difference? Instead of returning the outpout of the command, it will return true (the command starts successfully), false (unsuccessfully) or nil (the command did not complete). Due to the fact that the output of the command is not redirected to the return statement running tail -f , it will print the contents to standard output.
If you're good at getting results on standard output, you can just put it in a Thread block. This way, the growing content of filename written to standard output, and you can continue to run other Ruby code.
Thread.new { system('tail -f filename') }
If you want complete control, write down the output to save it for searching at another point in your script, and then look at the answer to the following question that describes this approach:
Read continuously from STDOUT of an external process in Ruby
It is based on the PTY module, which creates and manages pseudo-terminals. Basically, you can create another terminal through this module. Then the code might look like this:
require 'pty' cmd = "tail -f filename" begin PTY.spawn(cmd) do |stdin, stdout, pid| begin
Finally, there is also a socket approach. Open the socket in Bash or using socat , output the tail -f output to the socket and read it in Ruby from the socket.