In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use the logrotate of Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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. In addition, old logs can also be sent by e-mail, but this option is beyond the scope of this tutorial.
Logrotate packages are installed by default on major Linux distributions, and if for some reason logrotate does not appear in it, you can use the apt-get or yum command to install it.
On Debian or Ubuntu:
# apt-get install logrotate cron on Fedora,CentOS or RHEL:
The configuration file for # yum install logrotate crontabslogrotate is / etc/logrotate.conf, which usually does not need to be modified. The round robin of the log file is set in a separate configuration file, which is placed in the / etc/logrotate.d/ directory.
Example 1 in the first example, we will create a log file / var/log/log-file for 10MB. We will show how to use logrotate to manage the log file.
Let's start by creating a log file and fill it with 10MB's random bitstream data.
# touch / var/log/log-file# head-c 10m / var/log/log-file since the log file is now ready, we will configure logrotate to take turns on the log file. Let's create a configuration file for this file.
# vim / etc/logrotate.d/log-file/var/log/log-file {monthly rotate 5 compress delaycompress missingok notifempty create 644 root root postrotate / usr/bin/killall-HUP rsyslogd endscript} here:
Monthly: log files are rotated on a monthly basis. Other available values are 'daily','weekly'' or 'yearly'.
Rotate 5: five archive logs will be stored at a time. For the sixth archive, the oldest archive will be deleted.
Compress: after the round robin task is completed, the rotated archives will be compressed using gzip.
Delaycompress: always used with the compress option, which instructs logrotate not to compress the most recent archive, which will occur in the next round robin cycle. This is useful when you or any software still needs to read the latest archives.
Missingok: during log rotation, any errors, such as "file cannot be found", will be ignored.
Notifempty: if the log file is empty, the round robin will not occur.
Create 644 root root: creates a new log file with the specified permissions, and logrotate renames the original log file.
Postrotate/endscript: after all other instructions are completed, the commands specified in postrotate and endscript will be executed. In this case, the rsyslogd process will immediately read its configuration again and continue to run.
The above template is generic, while the configuration parameters are adjusted according to your needs, not all parameters are necessary.
Example 2 in this case, we only want to take a log file, but the log file size can grow to 50MB.
# vim / etc/logrotate.d/log-file/var/log/log-file {size=50M rotate 5 create 644 root root postrotate / usr/bin/killall-HUP rsyslogd endscript} sample 3 We want the old log file to be named after the creation date, which can be achieved by adding dateext Changshu.
# vim / etc/logrotate.d/log-file/var/log/log-file {monthly rotate 5 dateext create 644 root root postrotate / usr/bin/killall-HUP rsyslogd endscript} this will allow archive files to include date information in their filenames.
Troubleshooting some troubleshooting tips for logrotate settings are provided here.
1. Running logrotatelogrotate manually can be invoked manually from the command line at any time.
To invoke logrotate for all logs configured under / etc/lograte.d/:
# logrotate / etc/logrotate.conf calls logrotate for a specific configuration:
# logrotate / etc/logrotate.d/log-file2. The best choice during the walkthrough is to use the'- d 'option to run logrotate in rehearsal mode. To verify, without actually taking any log files, you can simulate the walkthrough log round robin and display its output.
# logrotate-d / etc/logrotate.d/log-file
As we can see from the output above, logrotate determines that this round-robin is unnecessary. This will happen if the file time is less than one day.
3. Force Round Robin even if the round-robin condition is not met, we can force logrotate to round-robin log files by using the'- f 'option, and the'-v 'parameter provides detailed output.
# logrotate-vf / etc/logrotate.d/log-filereading config file / etc/logrotate.d/log-filereading config info for / var/log/log-fileHandling 1 logsrotating pattern: / var/log/log-file forced from command line (5 rotations) empty log files are rotated, old logs are removedconsidering log/ var/log/log-file log needs rotatingrotating log/ var/log/log-file Log- > rotateCount is 5dateext suffix'- 20140916'glob pattern'- [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] [0-9] 'renaming / var/log/log-file.5.gz to / var/log/log-file.6.gz (rotatecount 5, logstart 1, I 5) Old log/ var/log/log-file.5.gz does not existrenaming / var/log/log-file.4.gz to / var/log/log-file.5.gz (rotatecount 5, logstart 1, I 4), old log/ var/log/log-file.4.gz does not exist. . .renaming / var/log/log-file.0.gz to / var/log/log-file.1.gz (rotatecount 5, logstart 1, I 0), old log/ var/log/log-file.0.gz does not existlog / var/log/log-file.6.gz doesn't exist-- won't try to dispose of itrenaming / var/log/log-file to / var/log/log-file.1creating new / var/log/log-file mode = 0644 uid = 0 gid = 0running postrotate scriptcompressing log with: / bin/gzip4. Logging for Logrotate the logs for logrotate itself are usually stored in the / var/lib/logrotate/status directory. If we want logrotate to log to any specified file for troubleshooting purposes, we can specify it from the command line as follows.
# logrotate- vf-s / var/log/logrotate-status / etc/logrotate.d/log-file5. Logrotate scheduled tasks logrotate required cron tasks should be automatically created at installation time, I posted the contents of the cron file for your reference.
# cat / etcstatus cron.dailypacer logrotatekeeper status bind Clean non existent logfile entries from status filecd sh# / var/lib/logrotatetest-e status | | touch statushead-1 status > status.cleansed 's / "/ / g 'status | while read logfile datedo [- e" $logfile "] & & echo"\ "$logfile\" $date "done > > status.cleanmv status.clean statustest-x / usr/sbin/logrotate | | how to use logrotate in exit 0/usr/sbin/logrotate / etc/logrotate.conf" Linux This is the end of the introduction of "use". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.