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 solve the problem of too large log files under Linux

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

Share

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

This article mainly introduces how to solve the problem of too large log files under Linux. It is very detailed and has a certain reference value. Interested friends must finish reading it!

How to make an application execute in the background:

Linux background execution command: & and the use of nohup

Through the above methods, the log of the application will be output to the log file myout.txt, which will continue to grow, eating away at the server's hard disk.

However, we all know that logs are very important to us. As soon as there is a bug in the application, or the server goes down, we must use the log file for debug or further analysis. Therefore, log files can not be simply deleted.

At this point, we thought, if only we could split the log files, so that we could leave important logs and delete unnecessary ones. Here is a good suggestion to describe this method in detail.

We can split the log every day, so in this way, in order to avoid confusion, the log we split should be dated. Of course, the date can be obtained by 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, for example: 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, log files are cut according to 60m size, which can be customized. -d-a 4 indicates that the file suffix is 4 digits. After we cut the documents, we have to number them in order, such as 0000Thelm 0001jcm0002. This 4 represents the number of digits.

After that,. / log/log$ {current_date} is the prefix of the sliced log file, which contains the current date. So, the final output format is similar to: log_20181005_0001.

After the log file is cut, you can delete the log file, otherwise it will lose the meaning of cutting the file. Deletion can be done in the following ways:

Cat / dev/null > nohup.out

Write the above commands in a script and run it every day, you can cut the log file into several sections, which is easy for us to check. The complete code is as follows:

#! / bin/bash current_date= `date-d "- 1 day" +% Y%m%d `date-b 65535000-d-a 4 / home/alvin/myout.txt / home/alvin/log/log_$ {current_date} _ cat / dev/null > nohup.out are all the contents of this article entitled "how to solve the problem of too large log files under Linux". Thank you for reading! Hope to share the content to help you, more related knowledge, 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