Friday, January 30, 2015

Watching for updates in log files

When programs are writing log files, you can watch them with any editor you like, cat, vi or emacs. This is sometimes necessary when programs are running in the background, or through some job management system, or as daemons etc..
But what about the updates?
Luckily, there is tail to do that for you. So say you want to monitor a file called output.txt. Then just use

tail -f output.txt 

instead of cat. Now tail just sits there and waits for data to come in, and prints it. Easy. When you're done you're just pressing ctrl+c, and tail aborts (of course not your program).

Now when you have a longer file, tail only prints the last couple lines and then waits for data. If you want to see the whole file and updates, use

tail -f output.txt -c +0

This might be a bit too much for very long files, so there's also the option -n, e.g.

tail -f output.txt -n 100

Which prints the last 100 lines and waits for new data.