The alert pipeline went quiet on a Sunday. Quiet is the worst sound a monitoring system can make, because it looks exactly like a good day.
The pipeline was a shell loop someone wrote years ago: tail -f /var/log/app/access.log | while read line; do ...; done, matching error patterns and paging on them. It had run for 400-odd days. Sunday night the app threw a real burst of 5xx, the log filled with them, and nothing paged. The loop was alive. ps said so. It was just reading a file nobody was writing to anymore.
-f follows a descriptor, not a name
tail -f opens the file once and keeps reading from that open file descriptor. It does not care what the path points at afterwards. When rotation renames access.log to access.log.1 and creates a fresh access.log, the descriptor stays attached to the inode that is now called access.log.1. Rotation by rename moves the name, not the open file. So tail dutifully follows the old inode forever, and the app, which reopened the path on SIGHUP, writes to the new one.
GNU tail documents this: -f is --follow=descriptor, and -F is --follow=name --retry. With -F it checks whether the name now refers to a different file and reopens. BSD tail is the same idea: -F follows the name across rotation, -f does not. Check your own man page rather than trusting me or a blog post. I have been wrong about tail flags in public before and the BSD and GNU pages differ in the details.
This is the same family of bug as sed -i turning a symlink into a file: the name and the inode are two different things, and a tool that holds one while the world changes the other will lie to you with a straight face.
Proving it
Do not guess. Look at what tail has open.
On FreeBSD, fstat -p <tailpid> lists the inode of the file it holds. On Linux, ls -l /proc/<pid>/fd shows the path with (deleted) if it was unlinked, or the rotated name if it was only renamed. Compare that inode with ls -i /var/log/app/access.log. Different numbers means tail is reading a corpse.
Then check the rotation config. Ours used create mode in logrotate: rename the old file, make a new one, signal the app. That is exactly the case -f loses. If it had been copytruncate, the inode would have stayed the same and -f would have survived, with its own different problem: tail has to notice the file shrank, and any lines written between the copy and the truncate are lost. On BSD, newsyslog does the rename and signal dance the same way by default.
The rotation had always run weekly, at roughly the same hour. The loop had been silent every Sunday after the rotation for as long as it existed. Nobody noticed because Sunday night is when nothing happens. The one week something did happen, we found out.
The fix
1. Use tail -F for anything that outlives a rotation. It is one capital letter. It reopens by name and survives create-style rotation. Note that there is a small window during the swap where lines can be missed or the name briefly does not exist; -F retries, but it is not a guarantee of zero loss.
2. Better, stop scraping logs with a shell loop. If the thing must page, have the app or a supervised shipper emit the event, and give the shipper a position file so it resumes by inode and offset. A pipe with no memory is a pipeline with no memory of what it missed. I covered reading what is going on when the dashboards are down, and a tail loop is a fine thing to do by hand at 3am. It is a terrible thing to leave running for four hundred days.
3. Alert on silence. The real failure was that “no matches” and “no input” looked identical. Add a heartbeat: if the loop has read zero lines in N minutes on a service that normally logs constantly, that is its own page. This is the lesson from the alert that fired for eight months run in reverse: an alert that never fires is just as broken, and harder to spot.
The rule
A monitor that cannot fail loudly will fail quietly. tail -f is a tool for watching a file you are looking at. The moment it runs unattended, you need -F, a heartbeat, or both. We now have both, and a comment in the script that says why, which is the closest I get to writing documentation.
More like this live in the reliability hub.
