Home  >  Article  >  System Tutorial  >  Quickly solve Linux log problems - a collection of practical tools

Quickly solve Linux log problems - a collection of practical tools

WBOY
WBOYforward
2024-02-10 11:00:191095browse

In Linux system operation and maintenance, logging is a very important part. It can help us deeply understand the cause of the problem and repair it when there is a problem with the system. But for beginners, viewing and solving log-related issues can be very difficult and tedious. Today, we will introduce you to several practical tools that can help you quickly solve Linux log problems.

Quickly solve Linux log problems - a collection of practical tools

We all know that logs are very important to us. Once a bug occurs in an application or the server crashes, we must use log files for debugging or further analysis. Therefore, the log file cannot simply be deleted.

At this time, we thought, it would be great if we could split the log files, so that we could keep important logs and delete unnecessary logs. This method will be introduced in detail below.

We can split logs every day. If so, in order to avoid confusion, the logs we split should all have dates. Of course, we can get the date through the following statement:

current_date=`date -d "-1 day" "+%Y%m%d"` 

date -d "-1 day" means to get the date of the previous day, which means that if we operate today, we will cut yesterday's log. %Y%m%d is the specific date format, that is, the year, month, and day format, such as: 20181005.

Next, let’s cut the log.

split  -b 65535000 -d -a 4  myout.txt  ./log/log_${current_date}_ 

Among them, 65535000 is 60M, that is, the log file is cut according to the size of 60M, and the size can be customized. -d -a 4 indicates that the file suffix is ​​4 digits. After we cut the files, we need to number them in order, such as 0000, 0001, 0002...the 4 represents the number of digits.

The following ./log/log${current_date} is the prefix of the log file after cutting, and the current date is included in it. So, the final output format is similar to: log_20181005_0001.

After the log file is cut, the log file can be deleted, otherwise the meaning of cutting the file will be lost. The deletion method can be used in the following ways:

cat /dev/null > nohup.out 

Write the above commands in a script and run it every day to cut the log file into several parts for easy troubleshooting. The complete code is as follows:

#!/bin/bash 
current_date=`date -d "-1 day" "+%Y%m%d"` 
split -b 65535000 -d -a 4 /home/alvin/myout.txt   /home/alvin/log/log_${current_date}_ 
cat /dev/null > nohup.out 

The above is the detailed content of Quickly solve Linux log problems - a collection of practical tools. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete