Monitor Log Files in Real-time
Last Updated by Code Sport. Filed under linuxMonitor Logs Files in Real-time By default the tail command displays the…
By default the tail
command displays the last n=10 lines of any file[1]. Hence, this can be used to monitor any log file (e.g, Apache, MySQL, error logs, etc). The FreeBSD Tail man page has the most robust explanation on usage. Here’s the basic format:
$ tail -f /path_to_log/file.logWhere:
-f
tells the OS to keep the command “open” and output (stream) to the terminal any updates to the file[1]. Outputting to the terminal (screen) is known as displaying to the standard output.
tail
Display last 30 lines in file, while outputting updates to the terminal
:$ tail -f -n 30 /path_to_log/file.log
Furthermore, tail -f
may be filtered through grep
if you’re interested in specific requests[2]:
$ tail -f /path_to_log/file.log | grep 192.168.0.1
Are updates streaming too fast? Use inline bash scripting to add a 3 second delay[3]:
$ tail -f /path_to_log/file.log | while read line; do echo $line; sleep 3; done