Lot of already written to this question and probably need clarify some principes.
If you want wait to EOF while the monitor process reading lines (regardless what is it, e.g. tail
or anything other) you must specify the wait-interval, with other words, what is considered as "new lines" in the file.
Imagine:
- you will reach EOF, but in 100 microseconds new line arrived, the process will read it and reach another EOF. Belong this new line to the previous block of lines? (probably yes).
- And what if the new line arrive in 5 seconds? This is probably a new block of lines.
So, as you can see, the specifying the time what you considering for the "new block" of lines is abosolutely necessary. With other words, if you reach EOF multiple times in an specified time interval - it mean one EOF only. (like reach EOF twice in 100 microseconds).
Therefore the tail -f
itself using 1 seconds timeout as default, and in the GNU version you can change this with the -s
parameter. From the docs:
-s, --sleep-interval=N
with -f, sleep for approximately N seconds (default 1.0) between iterations; with
inotify and --pid=P, check process P at least once every N seconds
Also, you can check this in the source code of the tail.
For the the inotify
library and inotofy-tools
the principe is the same. (And tail, (depends on of your distribution) can use inotofylib
itself)). For using the inotifylib
must call the function
struct inotify_event* inotifytools_next_event (int timeout)
Your program should call this function or inotifytools_next_events()
frequently; between calls to this function, inotify events will be queued in the kernel, and eventually the queue will overflow and you will miss some events. (see here).
Again, the time-interval is essential (and all common tools defaults it to 1 sec).
About the solution what you tried with -n $line
. It can't work, because the tail never returns an empty line, when reaching EOF. The tail simply return the last line what got, and waits for the new lines (and checks them in specified time-intervals).
Summary:
- you must specify the timeout on what you want check the EOF condition (probably 1 second should be OK, if not - change the sleep time of the above scripts.
- all above solutions are working and are OK
- and finally:
Answers which don't use tail or while read will be accepted as long as
they are native bash commands and about one line.
isn't make any sense, because of the above.
Hope this helps.