Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use tail utility to view a log file that is frequently recreated

Tags:

I need a solution in creating a script to tail a log file that is recreated (with the same name) after it reaches a certain size.

Using "tail -f" causes the tailing to stop when the file is recreated/rotated.

What I would like to do is create a script that would tail the file and after it reaches 100 lines for example, then restart the command... Or even better to restart the command when the file is recreated?

Is it possible?

like image 364
gfunk Avatar asked Jul 26 '11 09:07

gfunk


People also ask

What would be a useful way to use the tail command to view log files?

To watch log files that get rotated on a daily base you can use the -F flag to tail command. The tail -F will keep track if new log file being created and will start following the new file instead of the old file.

How do I watch a file with tail?

The tail command is essentially used for showing the lines of a file from the end and hence the term 'tail'. You can use the -f option to follow the tail of a file, which means that it will keep on showing the new lines added to the file continuously. To stop the tailing of the log file, use Ctrl+C terminal shortcut.

What is the command used to see continuously updating log file?

As said, tail command is the most common solution to display a log file in real time. However, the command to display the file has two versions, as illustrated in the below examples. In the first example the command tail needs the -f argument to follow the content of a file.

How do I show the tail end of a log file?

Use the following simple syntax to show the tail end of a log file in real-time. You can also filter the log right at the command line using regular expressions: Requires Windows Powershell (duh!) Basic functionality but some 3rd party extensions are available. For example, you need multiple cmdlet windows to monitor multiple files

How do I use tail to track files in Linux?

Using tail to Track Files in Real-Time Tracking new text entries arriving in a file—usually a log file—is easy with tail. Pass the filename on the command line and use the -f (follow) option. tail -f geek-1.log

What is the tail command and how to use it?

The tail command makes it easy to view log entries as they are written in real-time. Jack Wallen shows you how to make use of this indispensable tool. One of the single most helpful tools in your Linux admin arsenal is log files. And with the open-source platform, there are quite a few different log files to view.

Is this the most recent version of log tail tool?

But for those of you yearning for simplicity, this just might be the log tail tool you’re looking for. The most recent update, however, is from back in 2009, and the most recent version, inotail 0.5, was released in 2007.


2 Answers

Yes! Use this (the retry will make tail retry when the file doesn't exist or is otherwise inaccessible rather than just failing - such as potentially when you are changing files):

tail -f --retry <filename> 

OR

tail --follow=name --retry 

OR

tail -F <filename> 
like image 66
evan Avatar answered Oct 26 '22 15:10

evan


try running

watch "tail -f" yourfile.log 
like image 28
xuuso Avatar answered Oct 26 '22 15:10

xuuso