How can I view log contents in real time on Linux? There are a number of tools available to help you output the contents of a file while it is being modified. The tail command is the most commonly used.
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 this 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
Copy the code
The second example is the tailf command. It has a -f argument built in, so you don’t have to give it a -f argument.
$ sudo tailf /var/log/apache2/access.log
Copy the code
Typically, logs on Linux servers are rotation logs. In this case, you need the -f argument.
Tail -f monitors whether a new log is created (that is, a log file with the same name but a different FD) and displays the contents of the new log instead of the contents of the old file.
$ sudo tail -F /var/log/apache2/access.log
Copy the code
By default, however, the tail command only displays the last 10 lines of the file. If you only want to see the last two lines in real-time mode, use the -n and -f arguments together, as follows:
$ sudo tail -n2 -f /var/log/apache2/access.log
Copy the code
2. Multitail Command – Monitor Multiple Log Files in Real Time
Another interesting Command is Multitail Command. As the name suggests, it can monitor multiple logs in real time, and Multitail also lets you scroll back and forth through monitored files.
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]
Copy the code
The following example demonstrates how to display two log files simultaneously.
$ sudo multitail /var/log/apache2/access.log /var/log/apache2/error.log
Copy the code
3. lnav Command – Monitor Multiple Log Files in Real Time
Another command similar to Multitail is Lnav, which also monitors 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]
Copy the code
Using Lnav, you can view both logs as follows:
$ sudo lnav /var/log/apache2/access.log /var/log/apache2/error.log
Copy the code
4. less Command – Display Real Time Output of Log Files
Finally, you can view the log file with less, and then press Shift+F to view the log content in real time.
As with tail, holding Shift+F in less tracks the content at the end of the file. You can also call less with the +F argument.
sudo less +F /var/log/apache2/access.log
Copy the code
conclusion
Concern public number: programmer Bai Nannan. Get: Birdman’s Linux Home Dish