Tailing Multiple Files

 

This is a small command-line application I created a while back to monitor multiple application log files.  Sometimes it’s nice to see how log files from discrete but cooperating applications are sequenced. I was surprised to find that there wasn’t an option in GNU tail (or a variant) that would do exactly that.

So I created this tool and eventually cleaned it up enough to be worthy of a push. Since it relies on inotify and pthreads, it will probably only work with the Linux kernel.

Available Here:

https://github.com/maitkin/multi-tail

Synopsis:


./configure
make install
mtail

usage: mtail [OPTION]

Options:
-f	Path of file to watch, append an optional :PATTERN to only return matches
-s	Suppress filename prepending
-r	Reads and uses a list of path:patterns from given file
-h	Help - this message

 

Examples:
Tail two files:

mtail -f /var/log/messages -f /var/log/secure

 

Tail two files returning only matches for the given pattern

mtail -f /var/log/messages:audit -f /var/log/secure:disconnect

 

You can alternatively create a text file with lines of the format:

filename:[pattern]
...

The [pattern] is optional, but if included will only return lines if the string pattern matches.  You can then have mtail read the file via the -r option:

mtail -r <configfile>

Keep in mind that each filename:pattern creates a new thread. I found inotify to be quite powerful and easy to work with. Even if the file disappears and reappears (rolled) mtail will keep chunking along, thanks to the inotify interface.