Use tail -F, not tail -f, and add -n0 if you don’t want the last ten lines replayed at startup: tail -n0 -F /var/log/app.log. Everything below is unedited output from macOS 26.6 (BSD tail, BSD grep 2.6.0-FreeBSD); GNU-specific behaviour is quoted from the GNU coreutils tail(1) man page and was not run on Linux.
Alerting on ERROR lines: the naive loop breaks four ways
Do not use this monitoring recipe:
tail -f /var/log/application.log | while read line; do
echo "$line" | grep "ERROR" && echo "Error detected: $line" | mail -s "Error Alert" admin@example.com
done
1. read without -r and IFS= corrupts the line. Backslashes are eaten and leading/trailing whitespace is stripped, so the alert you get is not the log line that fired it. Two log lines in, two loops out:
input [ERROR path C:\Users\temp not found]
[ ERROR indented and spaced ]
while read line [ERROR path C:Userstemp not found]
[ERROR indented and spaced]
while IFS= read -r line [ERROR path C:\Users\temp not found]
[ ERROR indented and spaced ]
2. grep re-prints every matching line onto the script’s stdout, so a “silent” daemon spews the log back at you. 3. It forks echo, grep and mail once per line. A burst is a mail flood — five matching lines produced five separate messages:
MAIL-SENT -s Error Alert admin@example.com
MAIL-SENT -s Error Alert admin@example.com
MAIL-SENT -s Error Alert admin@example.com
MAIL-SENT -s Error Alert admin@example.com
MAIL-SENT -s Error Alert admin@example.com
count = 5
4. tail -f replays the last ten lines first, so every restart mails ten stale alerts:
tail -f old.log lines replayed at startup: 10
tail -n0 -f old.log lines replayed at startup: 0
The corrected version does the matching in the shell (no fork), preserves bytes, and rate-limits into one batched message:
#!/usr/bin/env bash
set -u
LOG=${1:?usage: alert.sh /path/to/app.log}
TO=${TO:-admin@example.com}
COOLDOWN=${COOLDOWN:-300} # min seconds between messages
TICK=${TICK:-5} # how often to wake and check
buf=$(mktemp); trap 'rm -f "$buf"' EXIT
n=0; last=$(date +%s)
flush() {
(( n == 0 )) && return
mail -s "[alert] $n ERROR line(s) in $LOG" "$TO" < "$buf"
: > "$buf"; n=0; last=$(date +%s)
}
tail -n0 -F "$LOG" 2>/dev/null | while :; do
if IFS= read -r -t "$TICK" line; then
case $line in
*ERROR*) printf '%s\n' "$line" >> "$buf"; n=$((n+1)) ;;
esac
else
(( $? > 128 )) || { flush; break; } # >128 = timeout; otherwise EOF
fi
(( $(date +%s) - last >= COOLDOWN )) && flush
done
Same five-line burst through the corrected version — one message, and the bytes survive:
MAIL-SENT -s [alert] 5 ERROR line(s) in live.log admin@example.com
count = 1
body:
[ERROR path C:\Users\temp gone]
[ ERROR indented ]
[ERROR three]
[ERROR four]
[ERROR five]
The read -t branch matters: on timeout read returns a status above 128 — the bash(1) manual documents that if read times out, it saves any partial input into the named variable and the exit status is greater than 128 — and the naive while IFS= read -r -t 5 line loop simply exits the first time the log goes quiet.
grep stalls whenever its output is not a terminal
tail -f access.log | grep PATTERN works at a prompt and appears to break the moment you build on it. The trigger is not the number of greps — it is whether grep‘s stdout is a terminal. It line-buffers to a TTY and block-buffers to a pipe or a file. Same writer, 2 seconds, only the destination changed:
run 1: grep->pty = 37 lines grep->file = 0 lines
run 2: grep->pty = 38 lines grep->file = 0 lines
run 3: grep->pty = 38 lines grep->file = 0 lines
So | grep A | grep B stalls because A now writes into a pipe, and redirecting a single grep to a file stalls for exactly the same reason. Both fixes work on BSD grep:
after 4s plain=0 --line-buffered=75 stdbuf-oL=75 (lines)
Use grep --line-buffered on every stage that feeds another process, or wrap a program with no such flag in stdbuf -oL. stdbuf ships with macOS 26 as well as GNU coreutils.
A -f monitor goes silent at rotation
-F keeps monitoring through rotation, and what -f costs you is the whole point. Rotating the way logrotate does — mv app.log app.log.1, recreate app.log — and then writing 12 markers into the new file, one per second, exactly as a live application would:
tail -F : 5/5 runs caught 12 of 12 markers, first = T1
tail -f : 5/5 runs caught 0 of 12 markers, first = NONE
-f produced nothing at all, in every run. That is the failure worth fearing, because an empty screen is indistinguishable from a quiet service. Deleting and recreating the file behaves the same way, also 5 runs each:
tail -f after rm+recreate : [] [] [] [] []
tail -F after rm+recreate : [AFTER-RECREATE|] x5
The one rotation style plain -f does survive is in-place truncation — logrotate’s copytruncate — because the inode never changes: [AFTER-TRUNCATE|] in 5 of 5 runs. If you do not control the rotation policy, you cannot rely on that. For a whole directory rather than one file, inotifywait watches the tree — subject to a per-user watch limit that silently truncates what it sees; tail -F has no such budget.
A measurement note, since it is an easy trap to fall into. Testing rotation by appending a single line to an otherwise idle file does not work: over 10 runs that shape reported the new file’s line twice and the rotated-away file’s line eight times, and -f and -F returned the same answer as each other in all 10. It measures how soon the kernel happens to notice the rename, not what the flags do, so it cannot distinguish them. Only a continuously-written file — the realistic case — separates the two. The rotation check in tail-follow-file.sh uses that same continuous-write shape and runs five times, as do the delete-and-recreate and truncation checks, and the mail-flood and grep-buffering examples are in it too, so allow a few minutes.
GNU’s documented default points the same way, and this is the half not executed on Linux. GNU tail(1): “-f, --follow[={name|descriptor}] output appended data as the file grows; an absent option argument means ‘descriptor'”, and “With --follow (-f), tail defaults to following the file descriptor, which means that even if a tail’ed file is renamed, tail will continue to track its end.” On Linux, then, logrotate’s default rename-and-create leaves tail -f holding the rotated-away file while the live log fills up unwatched — the same silence, reached by a different mechanism. GNU -F is documented as “same as --follow=name --retry“.
One flag covers every case on both platforms, which is why the command at the top of this page uses it: tail -n0 -F app.log.