Post Image

Linux Watch Log File in Real Time

Weather its the local syslog on your system or you have Linux syslog server and looking for logs to pull in, when troubleshooting something it can be beneficial to watch in real time as logs pull in so you don't have to keep going back and re issuing the command to view the latest logs. In this post I will show you the 1 command to continuously view logs in a log file that pull in.


Viewing Latest Logs in Log File

The first thing you need to know is how to view the latest logs in a file. The latest logs are going to be at the end of the file, so using a command like cat is not the best option because after just a few logs, the text will exceed the length of the screen, so in this case tail comes to the rescue.

$ tail /path/to/logfile

By default tail will show the last 10 lines of a file, this is not enough to be useful in my opinion, so I typically set the lines to something like 60 or 80 lines by using the -n argument.

$ tail -n 60 /path/to/logfile

One thing to note is that depending on the size of your screen you will be able to list less or more.


Auto Running Tail

This is the key piece, to automatically run the tail command on an interval to view the logs as they pull in, this is where the watch command is used. The watch command will take another command and all its arguments and run it on an interval, by default the interval is 2 seconds.

$ watch tail -n 60 /path/to/logfile

As I stated this will run the tail command every 2 seconds, for viewing log files I believe this is too slow, so I typically lower the interval to its lowest possible value .1 seconds.

$ watch -n .1 tail -n 60 /path/to/logfile

And your output you will see logs nearly as soon as they pull into your log file.



The watch command is not only useful for monitoring log files, it can be used for any command you want to run on an interval during a command session.


Comments (0)
Leave a Comment