2

I am trying to use tail utility in linux to monitor the logs present under nested directories. I tried using tail -f /var/log/**/* but this only go till direct child of log directory. It does not dig beyond one level. Basically I am trying to tail all the application logs in docker container and pass them to /proc/1/fd/1, so that they appear under docker logs.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Nish
  • 922
  • 13
  • 31
  • Which shell exactly? Modern Bash will recurse subdirectories with `**` so you must be using something else. – tripleee Sep 25 '19 at 15:08
  • Its bash. @tripleee – Nish Sep 25 '19 at 15:16
  • Some older version then? Which version? – tripleee Sep 25 '19 at 15:17
  • It's 4.3.48(1)-release @tripleee – Nish Sep 25 '19 at 15:18
  • And are you sure? Many people are surprised to learn that `/bin/sh` might not be Bash at all. – tripleee Sep 25 '19 at 15:18
  • echo $0 shows bash and echo $SHELL shows /sbin/nologin. I am running this inside my docker container. – Nish Sep 25 '19 at 15:19
  • tail(1) can’t do this. You need a dedicated tool capable of reading multiple files at once. Using a `docker run -v` option to mount a host directory over the container’s log directory is probably easier and would let you read the log files after the fact. – David Maze Sep 25 '19 at 15:24
  • @DavidMaze GNU `tail` will happily run with or without `-f` on any number of files you can fit into `ARG_MAX` (though if all of them are growing it will be hard to keep up). – tripleee Sep 25 '19 at 15:38

1 Answers1

5

You need to enable

 shopt -s globstar

if it is disabled in your shell.

With this setting enabled, Bash will recurse directories with **.

This is not a feature of Docker or tail, it is a feature of your shell.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • 1
    See also https://stackoverflow.com/questions/28176590/what-do-double-asterisk-wildcards-mean – tripleee Sep 25 '19 at 15:27
  • Thanks @tripleee , this shopt did the work for me. It was disabled. – Nish Sep 25 '19 at 17:09
  • If I wish to get the file name as well where process is tailing on in the output, what could be done for that? – Nish Sep 25 '19 at 17:22
  • When you run `tail -f` on multiple files, it tells you which one it's reading from when it switches from one to another. If that's unacceptable, I guess you need to run a separate process for each file and prefix each line with the file name, or something. There are dedicated tools like `multitail` which do this sort of thing (not sure what the exact feature set of that specific one is, just that it's popular). – tripleee Sep 25 '19 at 17:26
  • I made it work by below command `tail -f -v | awk '/^==> / {a=substr($0, 5, length-8); next} {print a":"$0}'` – Nish Sep 25 '19 at 18:11