On my linux system (GNU coreutils 8.12), I was able to check (using strace
) that tail -f
¹ uses the lseek
system call to skip over most of the file quickly:
lseek(3, 0, SEEK_CUR) = 0
lseek(3, 0, SEEK_END) = 194086
lseek(3, 188416, SEEK_SET) = 188416
This means that the size of the tracked file should not matter in anyway.
Maybe you can check if the same applies on your system. (Obviously, it should be the case.)
—
1. I also tried disabling inotify support with the undocumented ---disable-inotify
, just in case.
1) I'm not sure if the subshell (or backgrounding) is useful here:
sudo sh -c '( tail -n1 -f /path/to/nameOfLog.log & ) | grep -q "Started .*Application"'
shouldn't a simple pipe do?
sudo sh -c 'tail -n1 -f /path/to/nameOfLog.log | grep -q "Started .*Application"'
2) Your "try 1":
sudo timeout 5 grep -q "Started .*Application" <(tail -n1 -f /path/to/nameOfLog.log &)
expands the input redirection <()
on the command line that runs the sudo
, not inside sudo
. The filehandle it opens isn't passed through sudo
to grep
, so grep
can't open the /dev/fd/63
pseudo-file.
Same thing about backgrounding the tail
here, it shouldn't be necessary.
3) And, as phk commented, your "try 2":
sudo sh -c 'timeout 5 grep -q "Started .*Application" <(tail -n1 -f /path/to/nameOfLog.log &)'
...explicitly runs sh
, and not bash
or any other more featureful shell. Plain standard sh
doesn't support <()
, and neither does dash
which is used as sh
on Debian and Ubuntu.
When you run su
instead, it runs root
's login shell, which is likely to be bash
on Ubuntu. But using both sudo
and su
is redundant, they're both made to elevate privileges, and after sudo
you're already running elevated privilege, so no need for su
. Instead, if you want to run a shell inside sudo
, just say explicitly which one:
sudo bash -c 'timeout 5 grep -q "Started .*Application" <(tail -n1 -f /path/to/nameOfLog.log &)'
Best Answer
Sort command and THE most recent log file
Why using
-r
(reverse sort order) than reaching end of output withtail
?Using normal sort order and take first entry would be quicker!
or
work fine.
Nota: I like to use
sed
because this command is present in/bin
, maybe before/usr
are mounted.would work but, as already said: inversing sort order, than dropping whole ouput for using only last entry is not a real good idea ;-)
Warning, in last directory,
*
have to not match a directory, or else commandtail
won't know how to open it.Same but using
find
for searching for most recent file:Note:
-mmin +-1
ensure to not list bad timed files: in the futur.read
is builtin, create an array and prevent the use ofhead -n1| cut -d \ -f2
-mmin -10
could be changed or suppressed, but this prevent long sort process.But tail support to watch about more than one file:
Try to open two shell console and try this:
In 1st console:
in second one, while keeping 1st console visible, hit many time:
1st console could look like:
...
In the idea of SO question, but time based, multi files
By using
find
command, we could watch on last minutes modified files-mmin
or last days-mtime
:for watching for logfiles modified last 10 minutes.
Note:
man tail
, about-F
option for long time watch-q
option for not printing file namesFancy formatting
Where you could modify
.path.to.logdir.
and change12
for more suitable length.For sample, keeping our two console, stop 1st and try
than in second console, hit again some