1

The problem with tail -f

tail -f is very useful to see real-time addition to log files. It allows watching all log files in specified folders, example: tail -f abc.log mydir/*.log

Problems:

  • abc.log is ignored forever if it does not exist at the moment the command is launched (which is most of the time, since I usually delete all log files before launching the tail -f command.
  • The * only expands at launch time, which means that if after 5 minutes a new file appears in the directory, it is ignored.

Requirements

  • Take file path(s) and folder path(s) as input
  • Output the content of these text files as soon as the content appears (as fast as possible)
  • For folders, keep checking whether new files have been created in them
  • For files that do not exist yet, keep checking whether they have been created
  • Free and open source
  • Bonus if maintained deb/rpm packages are available, double bonus if present in major distributions' channels.

Tailing several files could result in such output for instance if a.log is written then b.log then a.log again:

==> a.log <==
bla1
bla2

==> b.log <==
blb1
blb2

==> a.log <==
bla3
bla4

This is how tail does it, but similar formatting is OK too.

4
  • No direct recommendation of what fits it all, but a "keyword" to keep in mind: if you want new files to be considered, iNotify might be of help (ships in the repositories). You can have it watch folders or files for changes, and execute an action when those happen.
    – Izzy
    Jan 10, 2017 at 12:20
  • 1
    Does it have to be software? It's pretty easy to create in bash. Here's the one for one single file: while [ ! -f "MyFile" ]; do echo "waiting"; sleep 10; done; tail -f MyFile For your "For folders" requirement, you'll need to explain what you want to happen if/when there's more than one such file.
    – CPerkins
    Jan 10, 2017 at 14:28
  • @CPerkins: Yes that would be acceptable! Indeed it was unclear, I edited my question.
    – Nicolas Raoul
    Jan 11, 2017 at 2:42
  • 1
    Dude, that's fantastic. I've used bash for mumble years, use tail -f on a more-than-daily basis, and I somehow never learned that it can handle multiple files.
    – CPerkins
    Jan 11, 2017 at 14:25

1 Answer 1

3

Though the output is not exactly as tail -f, the multitail command, available in the package of the same name in several distributions, does quite well. The example

multitail  --retry -i abc.log -q 5 'mydir/*.log'

will retry an open on the first file, and every 5 seconds will test for new files matching the glob pattern, and tail -f them, showing each file in a window within your terminal. There are many configuration options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.