Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to monitor log files in real time in Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to monitor log files in real time in Linux. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

1. Tail Command-Monitor Logs in Real Time

As mentioned earlier, the tail command is the most common way to display logs in real time. However, there are two versions of the command, as shown below.

The first example is to add the-f argument to the tail command.

$sudo tail-f / var/log/apache2/access.log

The second example is the tailf command. It has a built-in-f parameter, so you no longer need to specify the-f parameter for it.

$sudo tailf / var/log/apache2/access.log

Usually the logs on the Linux server are rotational logs. In this case, you need to use the-F parameter.

Tail-F monitors whether a new log is created (the new log refers to a log file with the same name but a different fd) and instead displays the contents of the new log instead of the contents of the old file.

$sudo tail-F / var/log/apache2/access.log

By default, however, the tail command displays only the last 10 lines of the file. If you only want to view the last two lines in real-time mode, you can use the-n and-f parameters together, as follows:

$sudo tail-N2-f / var/log/apache2/access.log

2. Multitail Command-Monitor Multiple Log Files in Real Time

Another interesting command is Multitail Command. You can see from the name that it can monitor multiple logs in real time, and Multitail also allows you to flip through the monitored files back and forth.

Use the following command to install Mulitail on a Debian-or RedHat-based system.

$sudo apt install multitail [On Debian & Ubuntu] $sudo yum install multitail [On RedHat & CentOS] $sudo dnf install multitail [On Fedora 22 + version]

The following example shows how to display two log files at the same time.

$sudo multitail / var/log/apache2/access.log / var/log/apache2/error.log

3. Lnav Command-Monitor Multiple Log Files in Real Time

Another Multitail-like command is that Lnav,Lnav can also monitor multiple files in real time.

Use the following command to install Lnav on a Debian-or RedHat-based system.

$sudo apt install lnav [On Debian & Ubuntu] $sudo yum install lnav [On RedHat & CentOS] $sudo dnf install lnav [On Fedora 22 + version]

Using Lnav to view two logs at the same time is as follows:

$sudo lnav / var/log/apache2/access.log / var/log/apache2/error.log

4. Less Command-Display Real Time Output of Log Files

Finally, you can use the less command to view the log file, and then press Shift+F to view the log contents in real time.

Like tail, pressing Shift+F in less tracks the contents at the end of the file. You can also add the + F parameter when calling the less command.

Sudo less + F / var/log/apache2/access.log

The above is how to monitor log files in real time in Linux shared by Xiaobian. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report