In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to use logrotate, the housekeeper of Linux log files. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Logrotate is a very useful tool that automatically truncates (or rotates) logs, compresses, and deletes old log files. For example, you can set up logrotate to rotate the / var/log/foo log file every 30 days and delete logs for more than 6 months. After configuration, the operation of logrotate is fully automated without any further human intervention.
How does logrotate work
By default, the logrotate command, which runs once a day as a cron task in / etc/cron.daily, will help you set a policy in which log files that exceed a certain time or size are rotated. Command:
/ usr/sbin/logrotate
Configuration file: / etc/logrotate.conf, which is the main configuration file for logrotate. Logrotate also stores the configuration of a specific service in / etc/logrotate.d/. Make sure that the following line is included in / etc/logrotate.conf to read the specific service log configuration.
Include / etc/ logrotate.d`
Logrotate History: / var/lib/logrotate.status
Important logrotate options:
Compress-- > all non-current versions of the compressed log file daily,weekly,monthly-- > rotate the log file delaycompress according to the specified schedule-- > compress all versions except the current and next recent endscript-- > Mark the end of the prerotate or postrotate script errors "emailid"-- > send an error notification missingok to the specified mailbox-- > if the log file is lost Do not display error notifempty-- > if the log file is empty Do not rotate the log file olddir "dir"-- > specify that the old version of the log file is placed in "dir" postrotate-- > introduce a script prerotate that executes after the log is rotated-- > introduce a script rotate 'n' that is executed before the log is rotated-- > n versions of sharedscripts that include logs in the rotation scheme. -- > run the script size='logsize' only once for the entire log group-- > when the log size is greater than logsize (for example, 100K 4m) when rotating configuration
Let's configure logrotate for our own sample log file / tmp/sample_output.log.
Step 1: add a downline to / etc/logrotate.conf.
/ tmp/sample_output.log {size 1k create 700 root root rotate 4 compress}
In the configuration file above:
Size 1k-logrotate runs only when the file size is equal to (or greater than) this size. Create-rotates the original file and creates a new file with the specified permissions, users, and groups. Rotate-limits the number of log file rotation. Therefore, only the log files for the last 4 rotations will be retained. Compress-this will compress the file.
Step 2: usually, you need to wait a day for logrotate to be executed by / etc/cron.daily. In addition, you can run it on the command line with the following command:
/ usr/sbin/logrotate / etc/logrotate.conf
Output before executing the logrotate command:
[root@rhel1 tmp] # ls-l / tmp/total 28 RWMI. 1 root root 20000 Jan 1 05:23 sample_output.log
Output after executing logrotate:
[root@rhel1 tmp] # ls-l / tmptotal 12 RWX Murray. 1 root root 0 Jan 1 05:24 sample_output.log-rw-. 1 root root 599 Jan 1 05:24 sample_output.log-20170101.gz [root@rhel1 tmp] #
In this way, we can confirm that logrotate is successfully implemented.
Thank you for reading! This is the end of the article on "how to use logrotate, the housekeeper of Linux log files". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.