DEV Community

Cover image for Watch a Growing Log File with tail -f
Labby for LabEx

Posted on Originally published at labex.io

Watch a Growing Log File with tail -f

Originally published on LabEx Blog.

When a log gains a line after each request or job, rerunning tail makes you guess whether anything changed. tail -f leaves one terminal watching while the file grows. The useful habit is to separate the watcher from the writer, then stop the watcher without confusing that action with changing the file.

This short LabEx video uses two terminals and one small order log. The commands below reproduce that pattern in any disposable directory.

A LabEx terminal showing a new log entry appearing while a file is followed

Start with a file you can safely change

Use a directory you own. The first command makes a log with three known lines; the second prints its current end.

mkdir -p ~/tail-demo/logs
printf '%s\n' \
  '09:00 INFO cafe opened' \
  '09:05 ERROR milk unavailable' \
  '09:10 INFO milk restocked' \
  > ~/tail-demo/logs/orders.log
tail ~/tail-demo/logs/orders.log
Enter fullscreen mode Exit fullscreen mode

tail normally prints the last ten lines when no count is given. With -f, GNU tail prints the current end and then waits for appended data instead of returning to a prompt.

Let one terminal follow the file

Open Terminal A, change to the test directory, and run:

cd ~/tail-demo
tail -f logs/orders.log
Enter fullscreen mode Exit fullscreen mode

Terminal A should show the three records and then remain occupied. That missing prompt is the important state: tail is still running and ready to show an addition. It has not detected an error merely because it has no new line to print.

Leave that window alone. Open Terminal B in the same directory and append one complete record:

printf '09:15 INFO tea\n' >> logs/orders.log
Enter fullscreen mode Exit fullscreen mode

Terminal B returns to its prompt because the append is finished. Terminal A should print the new 09:15 line without rerunning tail -f. The video demonstration shows the two-window handoff and the difference between an occupied watcher and a terminal that is ready for another command.

Stop the watcher, then check the file

Return to Terminal A and press Ctrl+C. This interrupts tail in that terminal; it does not delete the file or undo the line Terminal B wrote. Check the last line with a separate command:

tail -n 1 logs/orders.log
Enter fullscreen mode Exit fullscreen mode

The output should still be:

09:15 INFO tea
Enter fullscreen mode Exit fullscreen mode

That final check is useful because it separates two facts: the follower stopped, and the file remains intact. If you later follow service logs that rotate, check the implementation's rotation behavior too. GNU tail -f follows a file descriptor by default; GNU documents tail -F as the shorthand for following a name with retry, which is often the behavior wanted when a log is renamed and recreated.

For terminal orientation, prompts, and running short commands before trying the two-window example, Linux Terminal Basics starts with guided environment, prompt, and command exercises. The course page currently describes those terminal fundamentals; it does not claim to be this log-following exercise.

References

Top comments (0)